Can't do a multiline assignment!

Gary Herron gherron at islandtraining.com
Thu Apr 17 11:30:29 EDT 2008


s0suk3 at gmail.com wrote:
> 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;
>   

Yuck!  No way!!  If you *want* to make your code that hard to read, I'm 
sure you can find lots of ways to do so, even in Python, but don't 
expect Python to change to help you toward such a dubious goal. 

Seriously, examine your motivations for wanting such a syntax.   Does it 
make the code more readable?  (Absolutely not.)  Does it make it more 
maintainable.  (Certainly not -- consider it you needed to change 
CONSTANT2 to a different value some time in the future.) 

Gary Herron


> 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