Line-continuation "Anti-Idiom" and with statement
Terry Reedy
tjreedy at udel.edu
Mon Nov 23 18:40:33 EST 2009
Neil Cerutti wrote:
> I installed Python 3.1 today, and I've been porting my small
> library of programs to the new system.
>
> I happened to read the interesting "Idioms and Anti-Idioms"
> HOWTO, and saw the '\' continuation character labeled an
> anti-idiom. I already generally avoided it, so I just nodded.
>
> Unfortunately, the new "nested" with statement (which I also read
> about today) forces me into this anti-idiom. When replacing an
> appearance of contextlib.nested with the 3K with statement, I
> ended up with an unexpected syntax error.
>
> with (open(roster_path, 'r') as roster_file,
> open(disb_path, 'w') as out_file,
> open(report_path, 'w') as report_file):
>
> The result was:
>
> File "C:\project\codxml.py", line 184
> with (open(roster_path, 'r') as roster_file,
> ^
> SyntaxError: invalid syntax
Right. The first open paren is illegal.
> Unless I'm missing something, I have to subject my code to a bit
> of contintuation:
>
> with open(roster_path, 'r') as roster_file,\
> open(disb_path, 'w') as out_file,\
> open(report_path, 'w') as report_file:
with open(roster_path, 'r') as roster_file, open(
disb_path, 'w') as out_file, open(report_path, 'w') as report_file:
would work ;-)
> I was thinking about submitting a enhancement request to the
> HOWTO, explaining that continuation my be required in this
> context. Am I missing anything obvious?
I would suggest replacing
"It is usually much better to use the implicit continuation inside
parenthesis:", <should be parentheses>
which says both too much and too little, with something like
"When the desired linebreak is within an expression, it is usually much
better to use implicit continuation inside parentheses, brackets, or
braces. If necessary, the whole expression can be surrounded by a new
pair of parentheses."
I would not mention 'with' specifically since there are many other
non-expression contexts where one cannot randomly add parentheses.
I believe that '\ \n' would always be harmless or a SyntexError outside
of expressons. I believe 'subtly wrong' only applies within expressions.
So I would not call \ continuation an anti-pattern outside
expressions. So you might suggest that the whole entry specify
expression context to begin with. To me, your example shows why blanket
condemnation is wrong.
You might separately request that with-item sequences be optionally
surrounded by parens, but I have a suspicion that this was considered
and rejected on either technical grounds and/or the grounds that \
continuation was purposefully left in the language in 3.x to be used
when implicit continuation is not appropriate, as in this situation.
The HOWTOs are not scripture.
Terry Jan Reedy
More information about the Python-list
mailing list