I’ve always seen magic (or dunder) methods as a way to implement the expected built-in behaviours in your own classes, in appropriate ways (rather than changing built-in behaviour). This allows your classes to play nice when duck typing is used.

For example, if your class contains some kind of sequence of values, you could implement the required methods to make it work like a list. That way, if you passed your object into an existing function that expected a list like object, it would work.

You would have to be very careful about changing the semantics so that your object started indexing at 1. Suppose your object is passed into a function that finds the length of the list and accesses elements between 0 and length-1? It would fail, and that might be very confusing to someone expecting your object to act like a list.

Likewise the __call__ method is mainly there to allow your object to behave like a function object. If you just want a method that changes the state of your object, it might be clearer to implement a normal method called change_state (or whatever).

Written by

I am a software developer with over 30 years experience in Java, Python and C++. I write for pythoninformer.com.

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