Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Advanced Objects Math

Andy McDonald
Andy McDonald
Python Development Techdegree Student 8,745 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

Mel Rumsey
.a{fill-rule:evenodd;}techdegree seal-36
Mel Rumsey
Treehouse Staff

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!