Home → Advanced → Context Managers
Reading
0%

📋 Table of Contents

  1. The Protocol
  2. Return Value Analysis

1 · The Protocol

📖
Textbook Definition

"Context manager objects exist to control a with statement, just as iterators exist to control a for statement. The protocol consists of __enter__ and __exit__ methods." — Luciano Ramalho

2 · Return Value Analysis

🔁 Return Value: __enter__

The value returned by __enter__ is what is bound to the target variable in the as clause. It is frequently self, but it can be any object (or None). Note that the context manager object and the return value of __enter__ are not necessarily the same.

📚 See Also