[Python-Dev] PEP 343 and __with__
jason.orendorff at gmail.com
Tue Oct 4 16:38:49 CEST 2005
The argument I am going to try to make is that Python coroutines need
a more usable API.
> Try to explain the semantics of the with statement without referring to the
> __enter__ and __exit__ methods, and then see if you still think they're
> superfluous ;)
> The @contextmanager generator decorator is just syntactic sugar [...]
> [T]he semantics of the with statement itself can
> only be explained in terms of the __enter__ and __exit__ methods.
That's not true. It can certainly use the coroutine API instead.
Now... as specified in PEP 342, the coroutine API can be used to
implement 'with', but it's ugly. I think this is a problem with the
coroutine API, not the idea of using coroutines per se. Actually I
think 'with' is a pretty tame use case for coroutines. Other Python
objects (dicts, lists, strings) have convenience methods that are
strictly redundant but make them much easier to use. Coroutines
with EXPR as VAR:
expands to this under PEP 342:
_cm = contextmanager(EXPR)
VAR = _cm.next()
Blah. But it could look like this:
_cm = (EXPR).__with__()
VAR = _cm.start()
I think that looks quite nice.
Here is the proposed specification for start() and finish():
class coroutine: # pseudocode
""" Convenience method -- exactly like next(), but
assert that this coroutine hasn't already been started.
raise ValueError # or whatever
""" Convenience method -- like next(), but expect the
coroutine to complete without yielding again.
except (StopIteration, GeneratorExit):
raise RuntimeError("coroutine didn't finish")
Why is this good?
- Makes coroutines more usable for everyone, not just for
- For example, if you want to feed values to a coroutine, call
start() first and then send() repeatedly. Quite sensible.
- Single mental model for 'with' (always uses a coroutine or
- No need for "contextmanager" wrapper.
- Harder to implement a context manager object incorrectly
(it's quite easy to screw up with __begin__ and __end__).
More information about the Python-Dev