Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMichael Braun
6,753 PointsPython - magic methods and override
I'm very new to programming and I am currently completing the python track. Kenneth keeps talking about override when it comes to magic methods but I don't really understand what that means. I feel like I have a very surface level understanding of what init() and new() does but I don't really know what's going on. I am just copying whatever he does in his videos to make the code work for the challenges, without understanding what I am actually doing. Would you guys mind explaining from your guys' point of view what override is and why it exists and what it does?
Thanks a lot!!
1 Answer
Alexander Davison
65,469 PointsMagic methods are methods that are automatically called by Python at the appropriate time. For instance, __init__
is called by Python when you create a new instance of that class. However, just because it is a "magic" method doesn't mean you can't call it yourself. You are allowed to something like object.__init__()
Overriding is the act of modifying the method that a child class inherited from a parent class.
All "base" classes are actually inherited from the special class object
, so when you "create" the method __init__
, it actually is overriding object
's method __init__