Next / Previous / Contents / TCC Help System / NM Tech homepage

26.3.11. __enter__: Context manager initialization

For a general explanation of context managers, see Section 23.9, “The with statement and context managers”. A class that acts a content manager must provide this special method as well as the one described in Section 26.3.12, “__exit__: Context manager cleanup”.

The expression that follows the word with in the with statement must evaluate to a context manager, whose .__enter__() method is called with no arguments (other than self). The value it returns will be bound to the variable named in the with statement's as clause, if one was provided.