Can't do a multiline assignment!

Grant Edwards grante at visi.com
Thu Apr 17 11:55:58 EDT 2008


On 2008-04-17, Gary Herron <gherron at islandtraining.com> wrote:

>> 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.)

You move the initialization of CONSTANT2 out of that construct
and set it to whatever you want.

Consider the case where you have this:

constant1 = <some complicated expression>
constant2 = <some complicated expression>
constant3 = <some complicated expression>
constant4 = <some complicated expression>
constant5 = <some complicated expression>
constant6 = <some complicated expression>

What happens when the initial value needs to change?  You have
to change it in six places instead of in one place as you would
if you did this:

constant1 = \
constant2 = \
constant3 = \
constant4 = \
constant5 = \
constant6 = <some complicated expression>

Having the same information be duplicated N times is bad.  In
C, the above construct can be used to solve that problem
efficiently.  In Python the right thing to do is probably this:

initialValue = <some complicated expression>
constant1 = initialValue
constant2 = initialValue
constant3 = initialValue
constant4 = initialValue
constant5 = initialValue
constant6 = initialValue

>> I find this limitation very annoying.

If you continue to try to write C code in a Python program,
you're going to continue to be annoyed.  Start writing Python
code when working on Python programs and writing C code when
working on C programs.  I write a lot of both and have no
problem switching back and forth other than an occasional
extraneous ";" in my python code.

Using an editor with good C and Python modes helps.

-- 
Grant Edwards                   grante             Yow! ... I want to perform
                                  at               cranial activities with
                               visi.com            Tuesday Weld!!



More information about the Python-list mailing list