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

Mike Meyer mwm-keyword-python.b4bdba at mired.org
Wed Sep 8 19:04:00 CEST 2010


On Wed, 8 Sep 2010 17:50:29 +0100
Mark Summerfield <mark at qtrac.eu> wrote:

> Hi,
> 
> I can't see a _nice_ way of splitting a with statement over mulitple
> lines:
> 
> class FakeContext:
>     def __init__(self, name):
>         self.name = name
>     def __enter__(self):
>         print("enter", self.name)
>     def __exit__(self, *args):
>         print("exit", self.name)
> 
> with FakeContext("a") as a, FakeContext("b") as b:
>     pass # works fine
> 
> 
> with FakeContext("a") as a,
>      FakeContext("b") as b:
>     pass # synax error
> 
> 
> with (FakeContext("a") as a,
>       FakeContext("b") as b):
>     pass # synax error

How about:

with FakeContext("a") as a:
 with FakeContext("B") as b:

If the double-indent bothers you, using two two-space indents might be
acceptable in this case.

       <mike
-- 
Mike Meyer <mwm at mired.org>		http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org



More information about the Python-ideas mailing list