What are the __enter__ and __exit__ methods in Python?
The __enter__ and __exit__ methods form Python's context manager protocol. Learn how with uses them and how to write your own context managers.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
The __enter__ and __exit__ methods form Python's context manager protocol. Learn how with uses them and how to write your own context managers.
Python calls __delattr__ whenever you run del obj.name. See how to override it safely, avoid recursion and protect attributes from deletion.
Learn what __setattr__ does in Python, how to use it for validation, logging and read-only objects, and how to avoid the infinite recursion trap.
__getattribute__ intercepts every attribute read on an instance. See how to use it safely, avoid recursion and compare it with __getattr__.
__contains__ defines the in and not in operators for your classes. See custom containers, the iteration fallback and the collections.abc helpers.
__next__ returns the next item of a Python iterator and raises StopIteration at the end. See custom iterators, the next() function and generators.
Learn how __iter__ and iter() work in Python, the difference between iterables and iterators, and how to build reusable iterables with generators.
Python calls __delitem__ for del obj[key]. See correct list-like and dict-like implementations, slice handling and which exceptions to raise.
Learn how __setitem__ powers square-bracket assignment in Python, with examples for custom lists, validated dictionaries and slices.
__getitem__ powers square-bracket access in Python. See how to build sequence-like and dictionary-like classes with slices and proper errors.
__lt__ and __gt__ define how your objects behave with the < and > operators. Learn how to implement them, return NotImplemented, and use total_ordering.
__eq__ defines what == means for your objects. See how to write it correctly, return NotImplemented, keep __hash__ consistent and use dataclasses.
__add__ defines the + operator and returns a new object, while __iadd__ defines += and updates the object in place. See examples and the pitfalls.
Learn how Python len() counts items in strings, lists, dicts and sets, why it fails on generators, and how to support it with __len__ in your classes.
Understand when Python calls __str__ and __repr__, how they fall back to each other, and how to write clear representations with examples.
The __call__ method makes instances callable like functions. See stateful callable objects, class-based decorators and how they compare with closures.
@classmethod passes the class as cls. Use it for alternative constructors and class-wide state, and compare it with staticmethod and instance methods.
Learn what @staticmethod does in Python, how it differs from instance and class methods, and when a module-level function is the better choice.
The @property decorator lets a method behave like an attribute, so you can validate input, compute values and keep your public interface stable as the class evolves.
Python's super() calls methods of the next class in the method resolution order. See how it works in __init__, overridden methods and multiple inheritance.