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.
Thomas Bynum4,616 Points
What is the point of a magic method?
I feel dense watching these videos, but what is the point of a magic method? I thought python knew how to add and subtract and convert to integers without me telling it how to do that. What is
doing differently than a + b?
Chris FreemanTreehouse Moderator 67,636 Points
Good question! All of the built-in types have magic methods like
__iadd__ predefined so it appears that things like
a+b just work. The types
float, etc. all have these magic methods.
When creating user-defined classes, how to “add” two instances of a class is at best unknown. By defining the requisite magic methods, the new class can take advantage of the + operator to do interesting things.
Post back if you need more help. Good luck!!!