__context__(self) => context object

Invoked when the object is used as the context expression of a with statement. The returned object must implement __enter__ and __exit__ methods.

Context managers written in Python can also implement this method using a generator function decorated with the contextlib.contextfactory decorator, as this can be simpler than writing individual __enter__ and __exit__ methods on a separate object when the state to be managed is complex.

With statement context objects also need to implement this method; they are required to return themselves (that is, this method should simply return self).