Can't do a multiline assignment!

s0suk3 at gmail.com s0suk3 at gmail.com
Thu Apr 17 16:59:51 CEST 2008


I was shocked a while ago when a discovered that in Python you can't
do a multiline assignment
with comments between the lines.

For example, let's say I want to assign a bunch of variables to an
initial, single value. In C or a similar language you would do:

CONSTANT1    =
/* This is some constant */
CONSTANT2    =
CONSTANT3    =

/*This is yet some other constant */
CONSTANT    =

1;

In Python, you usually can use parentheses to split something over
several lines. But you can't use parentheses for an assignment of
several lines. For that, you can use the line continuation character
('\'):

CONSTANT1    = \
CONSTANT2    = \
CONSTANT3    = \
1

But I realized that you can't do this:

CONSTANT1    = \
# Oops... syntax error
CONSTANT2    = \
CONSTANT3    = \ # This is also a syntax error
# And this too \
1

Does anyone know of a way to put the comments between lines like that?
I find this limitation very annoying.



More information about the Python-list mailing list