[Python-ideas] with statement syntax forces ugly line breaks?

Nick Coghlan ncoghlan at gmail.com
Thu Sep 9 14:53:37 CEST 2010

On Thu, Sep 9, 2010 at 10:08 PM, Georg Brandl <g.brandl at gmx.net> wrote:
> Am 09.09.2010 01:19, schrieb Greg Ewing:
>> MRAB wrote:
>>> On 08/09/2010 22:30, Nick Coghlan wrote:
>>>> I've sometimes wondered if we should consider the idea of making line
>>>> continuation implicit between keywords and their associated colons.
>>> If a colon was omitted by mistake, how much later would the parser
>>> report a syntax error?
>> It might be best to allow this only if the continuation
>> lines are indented at least as far as the starting line.
> That is dangerous, it makes the whitespace rules more complicated.

I'm actually not sure it is even *possible* in general to implement my
suggestion given the deliberate limitations of Python's parser.
Parentheses normally work their indentation-ignoring magic by dropping
down into expression evaluation scope where indentation isn't
significant (import is a special case where this doesn't quite happen,
but it's a rather constrained one).

This is definitely a wart in the with statement syntax, but it really
isn't clear how best to resolve it.

You can at least use parentheses in the individual context
expressions, even though you can't wrap the whole thing:

.>> from contextlib import contextmanager
.>> @contextmanager
... def FakeContext(a):
...   yield a
.>> with FakeContext(1) as x, (
...      FakeContext(2)) as y:
...   print(x, y)
1 2


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

More information about the Python-ideas mailing list