[Python-ideas] globals should accept parenteses for extending beyond 1 line

Nick Coghlan ncoghlan at gmail.com
Thu Jan 26 11:02:55 EST 2017


On 23 January 2017 at 22:29, MRAB <python at mrabarnett.plus.com> wrote:
> On 2017-01-23 20:09, Nick Timkovich wrote:
>>
>> Related and probably more common is the need for the line-continuation
>> operator for long/multiple context managers with "with". I assume that's
>> come up before, but was it also just a low priority rather than any
>> technical reason?
>>
> It has come up before, and there is a technical reason, namely the syntactic
> ambiguity when parsing. Not impossible to fix, but probably not worth the
> added complexity.

Right, it's the fact parentheses are already allowed there, but mean
something quite different:

    >>> with (1, 2, 3): pass
    ...
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: __enter__

These days, I'd personally be in favour of changing the parsing of
parentheses in that situation, as if we were going to add meaningful
context management behaviour to tuples we would have done it by now,
and having the name bindings next to their expressions is easier to
read than having them all at the end:

    with (cm1() as a,
             cm2() as b,
             cm3() as c):
        ...

Relative to tuples-as-context-managers, such an approach would also
avoid reintroducing the old resource management problems that saw
contextlib.nested removed and replaced with contextlib.ExitStack.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list