pyguides

Reference

Dunder Methods

Python Dunder Methods reference.

  1. __abs__ / __neg__ / __pos__

    Implement __abs__, __neg__, and __pos__ to control how Python's unary operators behave with your objects.

  2. __add__ / __radd__ / __iadd__

    Implement the + and += operators on your custom classes with __add__, __radd__, and __iadd__.

  3. __aenter__ / __aexit__

    Async context manager methods called by async with for resource setup and teardown. Define __aenter__ for entry and __aexit__ for exit logic.

  4. __aiter__ / __anext__

    The async iteration protocol pair. __aiter__ returns an async iterator; __anext__ returns an awaitable each step until StopAsyncIteration is raised.

  5. __await__

    The __await__ dunder method makes objects awaitable with await. Returns an iterator that Python drives to suspension and completion.

  6. __bool__

    Control how your objects evaluate in boolean context — Python's __bool__ method explained with practical examples.

  7. __call__

    Make instances callable like functions

  8. __class_getitem__

    A class method that handles subscription on the class itself, enabling generic type expressions like MyClass[T].

  9. __contains__

    Implement the __contains__ method to define custom membership test behavior for the in operator in Python.

  10. __copy__ / __deepcopy__

    Implement __copy__ and __deepcopy__ to control how copy.copy() and copy.deepcopy() handle your class instances.

  11. __del__

    Understand Python's __del__ destructor, its relationship to garbage collection, and why to avoid it in modern Python 3.

  12. __delitem__

    Learn how to implement __delitem__ to enable custom deletion behavior with the del keyword in your Python classes.

  13. __enter__ / __exit__

    A complete reference for Python's context manager protocol, covering __enter__ and __exit__ dunder methods, exception handling, and practical examples.

  14. __eq__ / __ne__

    Learn how to define equality and inequality for your Python objects using __eq__ and __ne__ dunder methods, with code examples.

  15. __format__

    Define custom string formatting for your objects. Controls how they appear in f-strings and format() calls.

  16. __get__ / __set__ / __delete__

    Control how attributes are accessed, assigned, or deleted on class instances with Python's descriptor protocol: __get__, __set__, and __delete__.

  17. __getattr__ / __getattribute__

    Control Python attribute access with __getattribute__ (every lookup) and __getattr__ (missing attributes only).

  18. __getitem__

    Implement subscription access for custom objects with __getitem__ — the hook behind obj[key] syntax in Python.

  19. __hash__

    Define the hash value for your Python objects so they can be used in sets and as dictionary keys.

  20. __init__

    Initialize instance attributes after object creation. Called automatically after __new__ completes.

  21. __init_subclass__

    A hook Python calls when a class inherits from one that defines it. Runs at class definition time, not instantiation.

  22. __iter__

    Learn how to implement __iter__ so custom Python objects work with for loops, iter(), list(), and other iteration tools.

  23. __len__

    Implement __len__ to define custom length behavior for your objects with Python's __len__ dunder method.

  24. __lt__ / __le__ / __gt__ / __ge__

    Learn how to define comparison operators for custom Python classes using __lt__, __le__, __gt__, and __ge__ rich comparison dunder methods.

  25. __matmul__ / __rmatmul__

    Implement the @ matrix multiplication operator with __matmul__, __rmatmul__, and __imatmul__ for custom objects in Python.

  26. __missing__

    Called when d[key] lookup fails on a dict subclass. Return a default value or raise KeyError.

  27. __mul__ / __rmul__ / __imul__

    Python's three multiplication dunder methods — when each is called, how NotImplemented works, and how __imul__ differs for mutables.

  28. __new__

    Creates and returns a new class instance before __init__ is called. Used to implement singletons, immutable objects, and factory patterns.

  29. __next__

    Learn how to implement the __next__ method to create custom iterators in Python. Complete guide with examples.

  30. __reduce__ / __reduce_ex__

    Control how Python serializes and reconstructs your objects during pickling, copying, and shelving.

  31. __repr__

    Return a string representation of an object for debugging. Defined automatically for all objects.

  32. __set_name__

    Called automatically when a class containing the descriptor is defined. Sets the owner class and attribute name on the descriptor instance.

  33. __setattr__ and __delattr__

    Control attribute assignment and deletion with Python's __setattr__ and __delattr__ dunder methods.

  34. __setitem__

    Python's __setitem__ method enables subscript assignment. Learn how to implement key-value setting in custom classes.

  35. __sizeof__

    Returns the size of an object in bytes. Measures the object's own memory footprint, not what it references.

  36. __slots__

    Declare fixed instance attributes in Python classes to eliminate __dict__ overhead and reduce memory usage.

  37. __str__

    Python's __str__ magic method explained with practical examples

  38. __sub__ / __rsub__ / __isub__

    Implement subtraction in Python classes: __sub__ for a - b, __rsub__ for reversed b - a, and __isub__ for in-place a -= b.

  39. __truediv__ / __floordiv__

    Implements true division (/) and floor division (//) operators. Covers return types, reversed variants, and common patterns.