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


Practical examples of magic methods. I don't get em'

I'm struggling to understand the practical advantages of these magic methods being described in the OO Python course. I'm still learning, but I don't 'get' what problems these solve. I'm muddling through the code challenges, but I'm never sure how I've managed, and just feel like I'm winging it. Can anyone offer any guidance here?

2 Answers

Magic methods are called "magic" because they don't have to be invoked directly. For example, the __init__ method is called whenever you assign an instance without having to specifically call it (foo = bar.__init__(arg)).

Basically, they're the methods that just works. When you do foo + foo you don't have to call foo.__add__ + foo.__add__, the class just knows to use the code defined by the __add__ method. It's magic because you can do whatever you want with the instance (foo) and as long as it's defined in a magic method, python just knows what to do, i.e. str(foo), foo * foo, foo - foo, len(foo), etc.

I think you're not realizing how useful this is because you might take for granted that 1 + 1 should just work, but under the hood, there's a magic method that tells python what the plus symbol means and what to actually do when someone "adds" two ints together or asks for the "len" of something.

Can you think of an example where the concept of adding might not be as intuitive? What does it mean to add two strings together? ('foo' + 'bar' = 'foobar') or a special class that makes integers words, adding would be different than adding strings (one + two = three). the magic methods defines what any of these methods do in different instances.

That makes sense. I guess it'll be more useful as I take on more real life scenario problems.

Ari Misha
Ari Misha
19,323 Points

Hiya Peter! Magic Methods are really fun in general , only once you get it of course. When i took Python course, i understood the concepts as soon as i saw the code , thats 'coz the concept of magic methods exists in PHP as well. But whats so fun about magic methods? First off, you cant invoke magic methods directly, the invocation happens behind the scenes , higher up in the Python object classes.

Lets consider an example of addition, aka + operator. Whenever you mention + in your methods or console or anywhere, you're actually overloading the operator. And there is a magic method for + called add(dunder add dunder). Now here comes the magic of Magic Methods, you can actually over-ride this add method in your class and make it do stuff on your own, like you could even make it subtract two numbers instead of adding. Thats where the true power of Magic Method lies. So yeah you instantiate the same class, now + will behave as however you implemented it in your add method. This is the basic concept behind magic methods. Now there are so many magic methods out there. And all of 'em are mentioned in Python Docs. I'd highly suggest you take time and practice 'em on your own and get your head wrapped around it.

Good luck!

~ Ari

(P.S. when i mentioned add in bold, i meant dunder add dunder)