[Python-Dev] Multiline ‘with’ statement line continuation

Ben Finney ben+python at benfinney.id.au
Tue Aug 12 01:27:57 CEST 2014


Allen Li <cyberdupo56 at gmail.com> writes:

> Currently, this works with explicit line continuation, but as all
> style guides favor implicit line continuation over explicit, it would
> be nice if you could do the following:
>
>     with (open('foo') as foo,
>           open('bar') as bar,
>           open('baz') as baz,
>           open('spam') as spam,
>           open('eggs') as eggs):
>         pass
>
> Currently, this is a syntax error

Even if it weren't a syntax error, the syntax would be ambiguous. How
will you discern the meaning of::

    with (
            foo,
            bar,
            baz):
        pass

Is that three separate context managers? Or is it one tuple with three
items?

I am definitely sympathetic to the desire for a good solution to
multi-line ‘with’ statements, but I also don't want to see a special
case to make it even more difficult to understand when a tuple literal
is being specified in code. I admit I don't have a good answer to
satisfy both those simultaneously.

-- 
 \           “We have met the enemy and he is us.” —Walt Kelly, _Pogo_ |
  `\                                                        1971-04-22 |
_o__)                                                                  |
Ben Finney



More information about the Python-Dev mailing list