Python Magic Methods

Built in classes define many magic methods, dir() function can show you magic methods inherited by a class.

Example:

This code displays the magic methods inherited by int class.

Python3




# code
print(dir(int))


Output

['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '_...

Or, you can try cmd/terminal to get the list of magic functions in Python, open cmd or terminal, type python3 to go to the Python console, and type:

>>> dir(int)

Output:

Dunder or magic methods in Python

Python Magic methods are the methods starting and ending with double underscores ‘__’. They are defined by built-in classes in Python and commonly used for operator overloading. 

They are also called Dunder methods, Dunder here means “Double Under (Underscores)”.

Similar Reads

Python Magic Methods

Built in classes define many magic methods, dir() function can show you magic methods inherited by a class....

Python Magic Methods

...

Dunder or Magic Methods in Python

Below are the lists of Python magic methods and their uses....