[Python-Dev] Proposed changes to PEP 343
jason.orendorff at gmail.com
Tue Oct 11 22:15:03 CEST 2005
On 10/7/05, Fredrik Lundh <fredrik at pythonware.com> wrote:
> the whole concept might be perfectly fine on the "this construct corre-
> sponds to this code" level, but if you immediately end up with things that
> are not what they seem, and names that don't mean what the say, either
> the design or the description of it needs work.
> ("yes, I know you can use this class to manage the context, but it's not
> really a context manager, because it's that method that's a manager, not
> the class itself. yes, all the information that belongs to the context are
> managed by the class, but that doesn't make... oh, shut up and read the
Good points... Maybe it is the description that needs work.
Here is a description of iterators, to illustrate the parallels:
An object that has an __iter__ method is iterable. It can plug
into the Python 'for' statement. obj.__iter__() returns an
iterator. An iterator is a single-use, forward-only view of a
sequence. 'for' calls __iter__() and uses the resulting
iterator's next() method.
(This is just as complicated as PEP343+changes, but not as
mindboggling, because the terminology is better. Also because
we're used to iterators.)
Now contexts, per PEP 343 with Nick's proposed changes:
An object that has a __with__ method is a context. It can plug
into the Python 'with' statement. obj.__with__() returns a
context manager. A context manager is a single-use object that
manages a single visit into a context. 'with' calls __with__()
and uses the resulting context manager's __enter__() and
A contextmanager is a function that returns a new context manager.
Okay, that last bit is weird. But note that PEP 343 has this oddness
even without the proposed changes. Perhaps either "context manager"
or contextmanager should be renamed, regardless of whether Nick's
changes are accepted.
With the changes, context managers will be (conceptually) single-use.
So maybe a different term might be appropriate. Perhaps "ticket".
"A ticket is a single-use object that manages a single visit into a
More information about the Python-Dev