Allowing comments after the line continuation backslash

Steven D'Aprano steve-REMOVE-THIS at cybersource.com.au
Mon Nov 1 04:02:37 EDT 2010


On Sun, 31 Oct 2010 23:13:19 -0700, Yingjie Lan wrote:

> Hi,
> 
> Sorry if I am baking too many ideas today. I am just having trouble with
> the backslashes....

Then don't use them.

If you are having so much trouble keeping your lines short, then I 
suggest you're trying to do too much in one line.


 
> I would like to have comments after the line continuation backslash.
> 
>>>> if a > 0 \ #comments for this condition
>       and b > 0:
>     #do something here

I'm actually sympathetic to the idea of having a backslash followed by 
any whitespace mean line continuation, allowing comments after the line. 
The problem is, what should you do here? Presumably if you follow the 
backslash with anything other than whitespace or a comment, you get a 
SyntaxError:

if a > 0 \ and b > 0:
    # do something


In any case, there is a moratorium on language changes, so it will 
probably be 2 or 3 years before this could even be considered.

What you can do instead:

if a > 0 and b > 0:
    # comment for a condition
    # comment for b condition
    do_something()

There's nothing wrong with that. But if you insist on in-line comments:

if (a > 0  # comment for a condition
    and b > 0  # comment for b condition
   ):
   do_something()


But best of all, write literate code that doesn't need comments because 
it documents itself:

if number_of_pages > 0 and not all(page.is_blank() for page in pages):
    do_something()



-- 
Steven



More information about the Python-list mailing list