Line continuation issue\

Peter Otten __peter__ at web.de
Fri Nov 4 11:38:32 EDT 2011


Steven Lehar wrote:

> Is this the right place to propose language extensions?
> 
> My Python code keeps expanding rightwards, it is difficult to keep it
> contained within reasonable limits. 

You should attack this by breaking large expressions into smaller ones and 
factoring out some of your code into helper functions.

> But the standard line continuation \
> is positively anti-Pythonic because an *invisible* white space between \
> and [CR] will render it useless.

Useless? You get a SyntaxError, remove the offending whitespace, and that's 
it.

> How about a new Python symbol, maybe \\ that CAN have following whitespace
> which is ignored, so that seeing a \\ in the code forces it to continue on
> the next line.

Did you know that there already is a pythonic alternative that works well in 
most cases? An open (, [, or { will allow you to continue on the next line, 
e. g.

items = [(foo(item), bar(item))
    for row in rows
    for item in row if baz(
        alpha=1,
        beta=item)]

no strings attached.




More information about the Python-list mailing list