[Python-ideas] ``with from`` statement

Andrew Svetlov andrew.svetlov at gmail.com
Sat Dec 22 18:11:09 CET 2012


Crazy idea.
Guido van Rossum mentioned after working on PEP 3156 that context
managers cannot use
yield from statement inside __enter__ and __exit__ magic methods.
Explicit call for entering and leaving context (for locking for
example) is not convenient.

What do you think about

with from f():
   do_our_work()


``with from ...` construction calls __enter_from__ generator  and
iterates via ``yield from`` for that.
Returned value is our context manager.

The same for __exit_from__ — do``yield from`` for that and stop on
StopIteration or exception.

--
Thanks,
Andrew Svetlov



More information about the Python-ideas mailing list