List sequential initialization

Steve Holden steve at holdenweb.com
Tue Jun 19 03:46:54 CEST 2007


Chris Mellon wrote:
> On 6/12/07, HMS Surprise <john at datavoiceint.com> wrote:
>> Thanks for the explaination. It didn't seem natural and from the
>> tutorial I read:
>>
>>     A value can be assigned to several variables simultaneously:
>>
>>     >>> x = y = z = 0  # Zero x, y and z
>>
>>
>> Maybe I infer too much....
>>
> 
> And yet, your answer is right there.
> 
> "A value can be assigned to several variables simultaneously"
> 
> When you say want a value assigned to several variables, Python
> doesn't assume that you actually mean you want 2 different values
> assigned to them.

The crucial difference between

a = b = "ab"
a = "a"

and

a = b = ['a', 'b']
a.append('c')

is that in the first case two names are bound to the immutable object 
"ab". Then the first name is rebound to a different immutable object.

In the second example, both names are bound to the same mutable object, 
a list. That object is then modified. The modification can be performed 
using either name, and both names continue to point to the same (but now 
mutated) object.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list