Which method must a class implement to be used as a context manager?

Python Professional Hard

Python Professional — Hard

Which method must a class implement to be used as a context manager?

Key points

  • Context managers require __enter__ and __exit__ methods.
  • __init__ and __del__ are not specific to context management.
  • __start__ and __stop__ are not standard context manager methods.

Ready to go further?

Related questions