[Tutor] (no subject)
Bob Gailer
bgailer at alum.rpi.edu
Tue Nov 30 01:29:50 CET 2004
At 04:58 PM 11/29/2004, Jeff Peery wrote:
>Hello,
>
>was wondering how python handles multi-line code. for example if I have a
>really long equation and I don't want to have to scroll over to look at
>it, can I just continue the equation on the next line? is this good
>practice? similarly can I do this for things like dictionaries and lists?
From the Python Language Reference:
2.1.5 Explicit line joining
Two or more physical lines may be joined into logical lines using backslash
characters (\), as follows: when a physical line ends in a backslash that
is not part of a string literal or comment, it is joined with the following
forming a single logical line, deleting the backslash and the following
end-of-line character. For example:
if 1900 < year < 2100 and 1 <= month <= 12 \
and 1 <= day <= 31 and 0 <= hour < 24 \
and 0 <= minute < 60 and 0 <= second < 60: # Looks like a valid date
return 1
A line ending in a backslash cannot carry a comment. A backslash does not
continue a comment. A backslash does not continue a token except for string
literals (i.e., tokens other than string literals cannot be split across
physical lines using a backslash). A backslash is illegal elsewhere on a
line outside a string literal.
2.1.6 Implicit line joining
Expressions in parentheses, square brackets or curly braces can be split
over more than one physical line without using backslashes. For example:
month_names = ['Januari', 'Februari', 'Maart', # These are the
'April', 'Mei', 'Juni', # Dutch names
'Juli', 'Augustus', 'September', # for the months
'Oktober', 'November', 'December'] # of the year
Implicitly continued lines can carry comments. The indentation of the
continuation lines is not important. Blank continuation lines are allowed.
There is no NEWLINE token between implicit continuation lines. Implicitly
continued lines can also occur within triple-quoted strings (see below); in
that case they cannot carry comments.
2.4.1 String Literals
...
In triple-quoted strings, unescaped newlines and quotes are allowed
Thus:
"""this is a
multiline string"""
That should cover it
Bob Gailer
bgailer at alum.rpi.edu
303 442 2625 home
720 938 2625 cell
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20041129/4f60fd56/attachment.htm
More information about the Tutor
mailing list