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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Thomas Bynum](https://secure.gravatar.com/avatar/a6574f5baad769519b706352ddde6559?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Thomas Bynum
4,616 PointsWhat 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 __add__
doing differently than a + b?
1 Answer
![Chris Freeman](https://uploads.teamtreehouse.com/production/profile-photos/1153532/micro_image.jpg)
Chris Freeman
Treehouse Moderator 68,425 PointsGood question! All of the built-in types have magic methods like __add__
, __radd__
, and __iadd__
predefined so it appears that things like a+b
just work. The types int
, str
, 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!!!
Thomas Bynum
4,616 PointsThomas Bynum
4,616 PointsThank you! So if you wanted to for some reason make + take the input and make it all capital letters and reverse it, you could? This is just giving us control over what the "normal" functions do?
Thanks for all your great feedback by the way. I've used your answers to so many questions to make sense of all this stuff.
Chris Freeman
Treehouse Moderator 68,425 PointsChris Freeman
Treehouse Moderator 68,425 PointsCorrect! The operator + and * call the methods
__add__
and__mul__
, respectively.To be really crazy, you could create a new class that inherited from
int
then override__add__
and__mul__
with each other’s definition so that + multiplied and * added! 😜Happy to help!