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 trial

Python Object-Oriented Python Advanced Objects Math

Andy McDonald
.a{fill-rule:evenodd;}techdegree seal-36
Andy McDonald
Python Development Techdegree Graduate 13,801 Points

Why do I have to call magic methods? Some appear to not have to be called and others do

If the beauty of magic methods is that python calls them on its own, why are we adding every conceivable method to this numstring class? Also, I'm a little confused as to why some of these would work without defining them within the class, and why others appear to need to be defined. For example, I can force anything into an int or string without adding the dunder method to the class, why is it that I can only swap the place of my numstring value after I explicitly add radd to the class?

1 Answer

Hey there Andy McDonald,

Kenneth is not explicitly calling the magic methods. He is overriding the defaults for these methods to customize the NumString class, but the interpreter calls them when the instance of the class is created and get called automatically when the instance is used mathematically.

Hopefully this helps!