[Python-ideas] with statement: multiple context manager

Chris Rebert pyideas at rebertia.com
Sun Mar 1 21:22:53 CET 2009

On Sun, Mar 1, 2009 at 11:53 AM, Eli Courtwright <eli at courtwright.org> wrote:
> On Sun, Mar 1, 2009 at 2:46 PM, Guido van Rossum <guido at python.org> wrote:
>> I am sympathetic to this desire -- I think we almost added this to the
>> original PEP but decided to hold off until a clear need was found.
> I second the motion to have this syntax added to the language.  I've
> often had to write nested with blocks to open one file for reading and
> another for writing.

It does seem slightly incongruous though, given that the
for-statement, which is quite similar to the with-statement in that
they both bind new variables in a subsidiary block of code, does not
directly support multiple simultaneous bindings.

To put it more concretely, currently one must write:

for a, b, c in zip(seq1, seq2, seq3):

Rather than:

for a in seq1, b in seq2, c in seq3:

But for some reason we're proposing to, in a way, make nested() built
into `with` but not make zip() likewise built into `for`.

While I still mostly like the idea, it does seem to undermine Python's
uniformity a bit.


Follow the path of the Iguana...

More information about the Python-ideas mailing list