[Python-ideas] with statement syntax forces ugly line breaks? (original) (raw)

Mark Summerfield mark at qtrac.eu
Wed Sep 8 18:50:29 CEST 2010


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

The use case where this mattered to me was this:

with open(args.actual, encoding="utf-8") as afh,
open(args.expected, encoding="utf-8") as efh: actual =
[line.rstrip("\n\r") for line in afh.readlines()] expected =
[line.rstrip("\n\r") for line in efh.readlines()]

Naturally, I could split the line in an ugly place:

with open(args.actual, encoding="utf-8") as afh, open(args.expected,
    encoding="utf-8") as efh:

but it seems a shame to do so. Or am I missing something?

I'm using Python 3.1.2.

-- Mark Summerfield, Qtrac Ltd, www.qtrac.eu C++, Python, Qt, PyQt - training and consultancy "Rapid GUI Programming with Python and Qt" - ISBN 0132354187 http://www.qtrac.eu/pyqtbook.html



More information about the Python-ideas mailing list