Trying to write comments after escaping newline

Peter Hansen peter at engcorp.com
Tue Apr 27 14:45:05 EDT 2004


p brian wrote:

> mystring = "hello " + someVariable + \     #comment
>                  "multiple lines 4 clarity" + \      #comment
>                  someothervariable                    #comment
> 
> it stops working because instead of escaping a newline I am now just
> escaping a space or tab.
> Is there some clever thing i have missed?

Yeah, don't do that.  :-)  Do this instead;

mystring = ("hello " + someVariable +     # comment
         "multiple lines 4 clarity" +      # another comment
         someOtherVariable)                # last comment

Better yet, consider finding a way to write your code that doesn't
require comments in the middle of strings like that.  It's not
necessarily a good idea to write comments just for comments' sake.
Try going for "self-documenting" code.

Finally, you could also just use string substitution to clean
this stuff up even more:

mystring = "hello %s multiple lines not needed %s" % (someVar, someOtherVar)

That's often easier on the eyes and the fingers than all those
concatenation operators.

-Peter



More information about the Python-list mailing list