__aenter__ — Easy Examples
Async version of __enter__ for async with blocks
Implementing __aenter__
Basic implementation of __aenter__ in a class.
python
class Example: def __aenter__(self): return "Example __aenter__" obj = Example() print(obj)
__aenter__ async version of __enter__ for async with blocks. Implementing it lets you customize how Python interacts with your objects.
__aenter__ in action
Seeing __aenter__ called by Python's built-in operations.
python
# How Python calls __aenter__ automatically class Demo: def __init__(self, value): self.value = value def __aenter__(self): print(f"__aenter__ was called!") return self d = Demo(42) # This triggers __aenter__: print(d)
Python automatically calls __aenter__ when you use the corresponding operator or function on your object.
Want to try these examples interactively?
Open Easy Playground