Implement __abs__, __neg__, and __pos__ to control how Python's unary operators behave with your objects.
Reference
Dunder Methods
Python Dunder Methods reference.
- __abs__ / __neg__ / __pos__
- __add__ / __radd__ / __iadd__
Implement the + and += operators on your custom classes with __add__, __radd__, and __iadd__.
- __aenter__ / __aexit__
Async context manager methods called by async with for resource setup and teardown. Define __aenter__ for entry and __aexit__ for exit logic.
- __aiter__ / __anext__
The async iteration protocol pair. __aiter__ returns an async iterator; __anext__ returns an awaitable each step until StopAsyncIteration is raised.
- __await__
The __await__ dunder method makes objects awaitable with await. Returns an iterator that Python drives to suspension and completion.
- __bool__
Control how your objects evaluate in boolean context — Python's __bool__ method explained with practical examples.
- __call__
Make instances callable like functions
- __class_getitem__
A class method that handles subscription on the class itself, enabling generic type expressions like MyClass[T].
- __contains__
Implement the __contains__ method to define custom membership test behavior for the in operator in Python.
- __copy__ / __deepcopy__
Implement __copy__ and __deepcopy__ to control how copy.copy() and copy.deepcopy() handle your class instances.
- __del__
Understand Python's __del__ destructor, its relationship to garbage collection, and why to avoid it in modern Python 3.
- __delitem__
Learn how to implement __delitem__ to enable custom deletion behavior with the del keyword in your Python classes.
- __enter__ / __exit__
A complete reference for Python's context manager protocol, covering __enter__ and __exit__ dunder methods, exception handling, and practical examples.
- __eq__ / __ne__
Learn how to define equality and inequality for your Python objects using __eq__ and __ne__ dunder methods, with code examples.
- __format__
Define custom string formatting for your objects. Controls how they appear in f-strings and format() calls.
- __get__ / __set__ / __delete__
Control how attributes are accessed, assigned, or deleted on class instances with Python's descriptor protocol: __get__, __set__, and __delete__.
- __getattr__ / __getattribute__
Control Python attribute access with __getattribute__ (every lookup) and __getattr__ (missing attributes only).
- __getitem__
Implement subscription access for custom objects with __getitem__ — the hook behind obj[key] syntax in Python.
- __hash__
Define the hash value for your Python objects so they can be used in sets and as dictionary keys.
- __init__
Initialize instance attributes after object creation. Called automatically after __new__ completes.
- __init_subclass__
A hook Python calls when a class inherits from one that defines it. Runs at class definition time, not instantiation.
- __iter__
Learn how to implement __iter__ so custom Python objects work with for loops, iter(), list(), and other iteration tools.
- __len__
Implement __len__ to define custom length behavior for your objects with Python's __len__ dunder method.
- __lt__ / __le__ / __gt__ / __ge__
Learn how to define comparison operators for custom Python classes using __lt__, __le__, __gt__, and __ge__ rich comparison dunder methods.
- __matmul__ / __rmatmul__
Implement the @ matrix multiplication operator with __matmul__, __rmatmul__, and __imatmul__ for custom objects in Python.
- __missing__
Called when d[key] lookup fails on a dict subclass. Return a default value or raise KeyError.
- __mul__ / __rmul__ / __imul__
Python's three multiplication dunder methods — when each is called, how NotImplemented works, and how __imul__ differs for mutables.
- __new__
Creates and returns a new class instance before __init__ is called. Used to implement singletons, immutable objects, and factory patterns.
- __next__
Learn how to implement the __next__ method to create custom iterators in Python. Complete guide with examples.
- __reduce__ / __reduce_ex__
Control how Python serializes and reconstructs your objects during pickling, copying, and shelving.
- __repr__
Return a string representation of an object for debugging. Defined automatically for all objects.
- __set_name__
Called automatically when a class containing the descriptor is defined. Sets the owner class and attribute name on the descriptor instance.
- __setattr__ and __delattr__
Control attribute assignment and deletion with Python's __setattr__ and __delattr__ dunder methods.
- __setitem__
Python's __setitem__ method enables subscript assignment. Learn how to implement key-value setting in custom classes.
- __sizeof__
Returns the size of an object in bytes. Measures the object's own memory footprint, not what it references.
- __slots__
Declare fixed instance attributes in Python classes to eliminate __dict__ overhead and reduce memory usage.
- __str__
Python's __str__ magic method explained with practical examples
- __sub__ / __rsub__ / __isub__
Implement subtraction in Python classes: __sub__ for a - b, __rsub__ for reversed b - a, and __isub__ for in-place a -= b.
- __truediv__ / __floordiv__
Implements true division (/) and floor division (//) operators. Covers return types, reversed variants, and common patterns.