When searching for a list of dunder methods with explanations, i.e., a cheat sheet on Python magic methods, I only found a couple of resources all over the web—each covering only a fraction of dunder methods. So, here’s my collection of 127 dunder methods (also called “magic methods”) from those sources, sorted alphabetically. I provide the data in three forms.
Python Dunder Methods Table with Explanation
Name | Description |
__abs__ | Absolute value of a given argument |
__add__ | Addition x + y for x and y arguments |
__aenter__ | Like __enter__() but must return an awaitable |
__aexit__ | Like __exit__() but must return an awaitable |
__aiter__ | Returns an asynchronous iterator |
__and__ | Bitwise “AND” of a and b |
__anext__ | Return an awaitable as the next value of the iterator argument |
__annotations__ | A dict containing annotations (values) associated to parameter names (keys) |
__await__ | Return an iterator to implement awaitable objects |
__bool__ | Truth value testing for built-in bool() returning False or True . If undefined, call __len__() |
__bytes__ | Called by bytes() to compute a byte-string representation of an object. Must return a bytes object. |
__call__ | Called when a given instance is called as a function |
__ceil__ | Implement math function ceil() |
__complex__ | Implement the built-in functions complex() to create a new complex number |
__contains__ | Implements the Python in operator to check membership. |
__del__ | Called when the instance is about to be destroyed |
__delattr__ | Delete an attribute |
__delete__ | Delete the attribute on an instance of the owner class. |
__delitem__ | Remove the value of the first argument at index as defined in second argument. |
__dir__ | Called when dir(x) is called on object x . |
__div__ | The division operator (/ ) in Python 2 is implemented by this dunder method. For Python 3, the __truediv__() method is used instead. |
__divmod__ | Implements the divmod() built-in method. Python’s built-in divmod(a, b) function takes two integer or float numbers a and b as input arguments and returns a tuple (a // b, a % b) . |
__enter__ | Enter the runtime context related to this object. |
__eq__ | Rich comparison: x==y calls x.__eq__(y) |
__exit__ | Exit the runtime context related to this object. |
__float__ | Called to implement the built-in function float() . |
__floor__ | Implements behavior for math.floor() , i.e., rounding down to the nearest integer. |
__floordiv__ | Implements a//b |
__format__ | The Python __format__() method implements the built-in format() function as well as the string.format() method. So, when you call format(x, spec) or string.format(spec) , Python attempts to call x.__format__(spec) . The return value is a string. |
__ge__ | Return whether x is greater than or equal y |
__get__ | Called on the attribute type to get a class attribute or instance attribute of the owner class. |
__getattr__ | Called when the default attribute access fails with an AttributeError |
__getattribute__ | Called unconditionally to implement attribute accesses for instances of the class. If the class also defines __getattr__() , this method won’t be called unless __getattribute__() either calls it explicitly or raises an AttributeError . |
__getitem__ | Return the value of a at index b . |
__gt__ | Returns the result of the greater than operation x > y |
__hash__ | Called by built-in function hash() , should return an integer. |
__hex__ | Does not work for Python 3. Use __index__() instead. |
__iadd__ | a = iadd(a, b) is equivalent to a += b . |
__iand__ | a = iand(a, b) is equivalent to a &= b . |
__idiv__ | a = idiv(a, b) is equivalent to a /= b in Python 2. In Python 3, this is replaced by __itruediv__ . |
__ifloordiv__ | a = ifloordiv(a, b) is equivalent to a //= b . |
__ilshift__ | a = ilshift(a, b) is equivalent to a <<= b . |
__imatmul__ | a = imatmul(a, b) is equivalent to a @= b . |
__imod__ | a = imod(a, b) is equivalent to a %= b . |
__import__ | Import a library by name. For example, to import the NumPy library dynamically, you could run __import__('numpy') . |
__imul__ | a = imul(a, b) is equivalent to a *= b . |
__index__ | Returns the object converted to an integer. This is used for many built-in functions such as oct() , hex() , or bin() . |
__init__ | Called after the instance has been created (by __new__() ), but before it is returned to the caller. |
__init_subclass__ | This method is called whenever the class defining it is subclassed. |
__instancecheck__ | Return True if instance should be considered a direct or indirect instance of class . If defined, called to implement isinstance(instance, class) . |
__int__ | Called to implement the built-in function int() . |
__invert__(x) | Return the bitwise inverse ~x of the number x . |
__ior__ | a = ior(a, b) is equivalent to a |= b . |
__ipow__ | a = ipow(a, b) is equivalent to a **= b . |
__irshift__ | a = irshift(a, b) is equivalent to a >>= b . |
__isub__ | a = isub(a, b) is equivalent to a -= b . |
__iter__ | This method is called when an iterator is required for a container. It returns a new iterator object that can iterate over all the objects in the container. |
__itruediv__ | a = itruediv(a, b) is equivalent to a /= b . |
__ixor__ | a = ixor(a, b) is equivalent to a ^= b . |
__le__ | Returns True if the former is less than or equal to the latter argument, i.e., x <= y |
__len__ | Called to implement the built-in function len() . Returns the length of the object >= 0. An object that doesn’t define __bool__() is considered False if its __len__() method returns zero. |
__lshift__ | Return x shifted left by y. |
__lt__ | Returns the result of the less than operation x < y |
__matmul__ | Return a @ b . |
__missing__ | Called by dict.__getitem__() to implement self[key] for dict subclasses when key is not in the dictionary. |
__mod__ | Return x % y. |
__mul__ | Return a * b, for a and b numbers. |
__ne__ | Rich comparison: x!=y and x<>y call x.__ne__(y) |
__neg__ | Return x negated (-x ). |
__new__ | Called to create a new instance of a given class cls . |
__next__ | Return the next item from the container. |
__oct__ | Does not work for Python 3. Use __index__() instead. |
__or__ | Return the bitwise or of a and b. |
__pow__ | Return a ** b, for a and b numbers. |
__radd__ | Called to implement the binary arithmetic operation + with reflected (swapped) operands. Only called if the left operand does not support the corresponding operation and the operands are of different types. |
__rand__ | Called to implement the binary arithmetic operation & (__and__ ) with reflected (swapped) operands. Only called if the left operand does not support the corresponding operation and the operands are of different types. |
__rdiv__ | Called to implement the binary arithmetic operation / (__div__ ) with reflected (swapped) operands. Only called if the left operand does not support the corresponding operation and the operands are of different types. |
__rdivmod__ | Called to implement the binary arithmetic operation divmod() with reflected (swapped) operands. Only called if the left operand does not support the corresponding operation and the operands are of different types. |
__repr__ | Called by the repr() built-in function to compute the “official” string representation of an object. |
__reversed__ | Called (if present) by the reversed() built-in to implement reverse iteration. It should return a new iterator object that iterates over all the objects in the container in reverse order. |
__rfloordiv__ | Called to implement the binary arithmetic operation // (__floordiv__) with reflected (swapped) operands. Only called if the left operand does not support the corresponding operation and the operands are of different types. |
__rlshift__ | Called to implement the binary arithmetic operation << (__lshift__) with reflected (swapped) operands. Only called if the left operand does not support the corresponding operation and the operands are of different types. |
__rmatmul__ | Called to implement the matmul operation @ (__matmul__) with reflected (swapped) operands. Only called if the left operand does not support the corresponding operation and the operands are of different types. |
__rmod__ | Called to implement the binary arithmetic operation % (__mod__) with reflected (swapped) operands. Only called if the left operand does not support the corresponding operation and the operands are of different types. |
__rmul__ | Called to implement the binary arithmetic operation * (__mul__) with reflected (swapped) operands. Only called if the left operand does not support the corresponding operation and the operands are of different types. |
__ror__ | Called to implement the binary arithmetic operation | (__or__) with reflected (swapped) operands. Only called if the left operand does not support the corresponding operation and the operands are of different types. |
__round__ | Called to implement the built-in function round() and math functions trunc() , floor() and ceil() . |
__rpow__ | Called to implement the arithmetic multiplication operation ** (__pow__ ) with reflected (swapped) operands. Only called if the left operand does not support the corresponding operation and the operands are of different types. |
__rrshift__ | Called to implement the binary arithmetic operation >> (__rshift__ ) with reflected (swapped) operands. Only called if the left operand does not support the corresponding operation and the operands are of different types. |
__rshift__ | Return a shifted right by b, i.e., a >> b . |
__rsub__ | Called to implement the binary arithmetic operation – (__sub__) with reflected (swapped) operands. Only called if the left operand does not support the corresponding operation and the operands are of different types. |
__rtruediv__ | Called to implement the binary arithmetic operation / (__truediv__) with reflected (swapped) operands. Only called if the left operand does not support the corresponding operation and the operands are of different types. |
__rxor__ | Called to implement the binary arithmetic operation ^ (__xor__) with reflected (swapped) operands. Only called if the left operand does not support the corresponding operation and the operands are of different types. |
__set__ | Called to set the attribute on an instance of the owner class to a new value. |
__set_name__ | Called at the time the owning class owner is created. The descriptor has been assigned to name. |
__setattr__ | Called when you assign an attribute via setattr() instead of the normal mechanism of storing the value in the instance dictionary. |
__setitem__ | Set a given element at a given index to a new value. |
__sizeof__ | Returns the internal size in bytes for the given object |
__str__ | Called by str(object) and the built-in functions format() and print() to compute the “informal” or printable string representation of an object. |
__sub__ | Return a - b . |
__subclasscheck__ | Return true if subclass should be considered a (direct or indirect) subclass of class. If defined, called to implement issubclass(subclass, class). |
__subclasses__ | Finds all subclasses of a given class. |
__truediv__ | Return a / b where 2/3 is .66 rather than 0. This is also known as “true” division. |
__trunc__ | Called to implement the math.trunc() function. |
__xor__ | Return the bitwise exclusive or of a and b. |
To get the list of sources used to create this table, please scroll down to the end of the article.
Python Special Attributes
Python has multiple special attributes that are defined per default for each class such as __name__
, __module__
, __dict__
, __bases__
, __doc__
, and __annotations__
.
Attribute | Type | Description |
---|---|---|
__name__ | str | The name of the class |
__module__ | str | The string name of the module where the class is defined |
__dict__ | dict | The dictionary with the namespace of the class |
__bases__ | tuple | A tuple with base classes of this class |
__doc__ | str or None | The documentation of the class as a string. If no documentation is defined, None . |
__annotations__ | dict | A dictionary with variable annotations in this class |
You can find them explained here:
Python Dunder Methods — One Method Per Line
__abs__ __add__ __aenter__ __aexit__ __aiter__ __and__ __anext__ __await__ __bool__ __bytes__ __call__ __ceil__ __class__ __class_getitem__ __cmp__ __coerce__ __complex__ __contains__ __del__ __delattr__ __delete__ __delitem__ __delslice__ __dict__ __dir__ __div__ __divmod__ __enter__ __eq__ __exit__ __float__ __floor__ __floordiv__ __format__ __fspath__ __ge__ __get__ __getattr__ __getattribute__ __getitem__ __getnewargs__ __getslice__ __gt__ __hash__ __hex__ __iadd__ __iand__ __idiv__ __ifloordiv__ __ilshift__ __imatmul__ __imod__ __import__ __imul__ __index__ __init__ __init_subclass__ __instancecheck__ __int__ __invert__ __ior__ __ipow__ __irshift__ __isub__ __iter__ __itruediv__ __ixor__ __le__ __len__ __length_hint__ __long__ __lshift__ __lt__ __matmul__ __metaclass__ __missing__ __mod__ __mro__ __mul__ __ne__ __neg__ __new__ __next__ __nonzero__ __oct__ __or__ __pos__ __pow__ __prepare__ __radd__ __rand__ __rcmp__ __rdiv__ __rdivmod__ __reduce__ __reduce_ex__ __repr__ __reversed__ __rfloordiv__ __rlshift__ __rmatmul__ __rmod__ __rmul__ __ror__ __round__ __rpow__ __rrshift__ __rshift__ __rsub__ __rtruediv__ __rxor__ __set__ __set_name__ __setattr__ __setitem__ __setslice__ __sizeof__ __slots__ __str__ __sub__ __subclasscheck__ __subclasses__ __truediv__ __trunc__ __unicode__ __weakref__ __xor__
Python Dunder Methods as a List of Strings
In case you need them as a Python list for copy&paste, here it is:
['__abs__', '__add__', '__aenter__', '__aexit__', '__aiter__', '__and__', '__anext__', '__await__', '__bool__', '__bytes__', '__call__', '__ceil__', '__class__', '__class_getitem__', '__cmp__', '__coerce__', '__complex__', '__contains__', '__del__', '__delattr__', '__delete__', '__delitem__', '__delslice__', '__dict__', '__dir__', '__div__', '__divmod__', '__enter__', '__eq__', '__exit__', '__float__', '__floor__', '__floordiv__', '__format__', '__fspath__', '__ge__', '__get__', '__getattr__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__hex__', '__iadd__', '__iand__', '__idiv__', '__ifloordiv__', '__ilshift__', '__imatmul__', '__imod__', '__import__', '__imul__', '__index__', '__init__', '__init_subclass__', '__instancecheck__', '__int__', '__invert__', '__ior__', '__ipow__', '__irshift__', '__isub__', '__iter__', '__itruediv__', '__ixor__', '__le__', '__len__', '__length_hint__', '__long__', '__lshift__', '__lt__', '__matmul__', '__metaclass__', '__missing__', '__mod__', '__mro__', '__mul__', '__ne__', '__neg__', '__new__', '__next__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__prepare__', '__radd__', '__rand__', '__rcmp__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rfloordiv__', '__rlshift__', '__rmatmul__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__set__', '__set_name__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__slots__', '__str__', '__sub__', '__subclasscheck__', '__subclasses__', '__truediv__', '__trunc__', '__unicode__', '__weakref__', '__xor__']
References and Sources
- https://www.reddit.com/r/Python/comments/br9ok2/list_of_all_python_dunder_methods/
- https://stackoverflow.com/questions/1418825/where-is-the-python-documentation-for-the-special-methods-init-new
- https://docs.python.org/3/reference/datamodel.html
- https://portingguide.readthedocs.io/en/latest/comparisons.html
- https://www.oreilly.com/library/view/python-in-a/0596001886/re25.html
- https://blog.finxter.com/python-divmod/