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

MRAB python at mrabarnett.plus.com
Thu Jan 26 20:26:22 EST 2017


On 2017-01-26 16:02, Nick Coghlan wrote:
> 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.
>
Just because the 'with' is followed by a '(', It doesn't necessarily 
mean that it's a tuple.

The 'as' is preceded by an expression, which could start with '('.

OTOH, I can't remember ever seeing the expression start with '('; it's 
usually the name of a callable.



More information about the Python-ideas mailing list