[Tutor] Multi-line statements with comments
Michael P. Reilly
Fri, 28 Dec 2001 17:51:44 -0500
On Fri, Dec 28, 2001 at 01:45:46PM -0800, Jim Angstadt wrote:
> Hi all,
> situation = [ 'win98se', 'python 2.1.1',
> 'very newbie user' ]
> An earlier thread pointed out to me the value of the
> string formatting operator. That was very helpful. It
> used this statement:
> print '<a
> href="%scgi=bin/commander.py?list=%s">%s</a>%s<p>' %
> (webpath, x, x, descriptor(x))
> [Somehow the continuation character, '\', was dropped.
> Maybe by my mail program.]
> However, the use of the continuation character does
> not permit one to have comments within the string. In
> regular expressions, there is a way to disreguard
> whitespace within the RE, and add comments, which
> makes a lengthy expression more readable. ( I'm
> referring to VERBOSE in python or x in perl. )
> What is a good technique for commenting multi-line
> statements that are not REs?
Line continuation cannot be used because the newline after it is, in
essence, being quoted by the backslash ('\').
But parentheses can be used to do this too. But do not compare what
new forms of regular expressions do with what a computer language does.
The RE takes a single string and parses it internally. Python won't
let you do the same within a string.
print ( # let's get busy
'<a href="%scgi-bin/commander.py?list=%s">%s</a>%s<p>' # base format
% (webpath, x, x, descriptor(x)) # rest of expression
But there IS a neat thing you can do with strings:
message = ('hi there\n' # notice no operator to join the strings
'how are you?\n' # no comma, no plus
'have a good day'# just the comment afterward
) # but we use the parentheses to get the multi-line
This makes an expression with only one value, no operators.
Syntactically, this is equivalent to:
message = 'hi there\nhow are you\nhave a good day'