Magic Methods in Python: Practical Applications

Ahmed Sakr
Aug 3 · 3 min read
Image for post
Image for post

Python is a versatile language. If you are making use of the Object-oriented (OO) capabilities, you will be happy to learn about magic methods, and how you can leverage them to your advantage. Let’s dive in!

Overriding default logic

Magic methods allow us to override default operator functionality or Class behavior on creation, initialization, and even destruction. A magic method is always prefixed and post-fixed by two underscores . If you’ve used Python for OO programming, you have likely used the magic method almost every time!

Let’s explore several magic methods with some practical examples.

The method

is called by python to create an object before is called to initialize the object. Use of is not needed in most cases as it can be abused to allow for anti-patterns in development. However, is specifically useful — and acknowledged by the official python documentation here — when you need to extend the behaviour of immutable types like tuples. Below is an example.

NumberTuple is a superset of tuple that allows for the specification of string numbers that ought to be converted to tuples. Since tuples are immutable — and is called after the tuple is already created — we must use to allow for the possibility of classes that extend immutable types. Neat, right?

The method

Printing objects of non-primitive classes are by default not very helpful to the user. They print the class type and memory address.

<__main__.Medium object at 0x7f7f07588c70>

In many cases when you’re building classes, it could be helpful to have an informative output of your object when you explicitly it. Thankfully, python allows us, once again, to override this default behaviour with our own liking.

The expects a string value to be returned. The returned string value will be the value shown on the screen when you attempt to print any object of the class!

The __repr__() method

You might have noticed that in our discussion of above, we asserted that it must be approached with a readability and informative stance. You want to give a good description to the user that will see the console. However, it shouldn’t expose any implementation details; specifically, it shouldn’t have to convey to the reader how the class operates.

But, as developers, we know good debugging is essential to testing and scaling applications. Having output that gets into the details of the implementation can help immensely when chasing a nasty bug. This is where fits in the picture: it stands for and its return value ought to give the developer a solid understanding of the current state of the object. Reusing our example from above, I will add a developer-friendly message that prints the internal id and interests count of the user.

Notice that while can easily be invoked through a regular , the message will only be derived when you invoke the built-in function (which Python understands as ‘grab the value).

The __mul__() method

Another category of magic methods is operator overriding: Python allows you to override the primitive operations like and with and , respectively. This is especially useful if you are creating mathematical classes that can benefit from the algebraic override. Here is a short example for creating a Dot-Product equipped vector class.

Having the ability to create complex classes that still work with basic operators like is a huge syntactic relief for any mathematician that is building complex mathematical models with Python!

That’s it for now. There are many more magic methods that I have not covered here, but I hope this brief introduction to magic methods sparked a curiosity in you to learn more about what Python allows you to override with default behaviour.

The Startup

Medium's largest active publication, followed by +733K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store