[Python-ideas] with-statement syntactic quirk

Bruce Leban bruce at leapyear.org
Thu Nov 1 23:44:12 CET 2012

On Thu, Nov 1, 2012 at 3:14 PM, Cameron Simpson <cs at zip.com.au> wrote:

> To my mind one of the attractive features of the current syntax is that
> forgetting the colon causes an immediate complaint.

I agree

I understand the attractiveness here, but I think I would prefer staying
> with the status quo (overt brackets or icky trailing sloshes) to extend
> the lines in a condition over opening the syntax to complaints far beyond
> the mistake.
Ditto except for the part about \ continuation being icky. I don't think
it's that bad. The two things that make \ continuation less attractive than
() continuation is that you can't put comments on the \-continued lines and
invisible trailing white space is a syntax error.

I don't understand the reason for either restriction.

with open('/etc/passwd') as p1, \      # source
     open('/etc/passwd') as p2:        # destination

seems more readable than

with open('/etc/passwd') as p1, \
     open('/etc/passwd') as p2:        # source, destination

A reasonable restriction (to my mind) would be to require at least two
spaces or a tab after a \ before a comment (although requiring just one
space would also be ok with me although I personally would always use
more). This change couldn't break existing code since \ is currently a
syntax error if followed by whitespace or a comment.

I would ignore whitespace after a final \ in a string, but would not allow

(Yes, I realize that better variable names would obviate the need for these
particular comments but comments are still useful sometimes :-)

--- Bruce
Follow me: http://www.twitter.com/Vroo http://www.vroospeak.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20121101/8b002d36/attachment.html>

More information about the Python-ideas mailing list