[Tutor] Python oddity
keridee at jayco.net
Thu Feb 28 02:56:04 CET 2008
Python lists are mutable. All mutable objects will behave in the fashion you
described, whereas immutable objects -- tuples, integer, floats, etc. --
will behave in the fashion that you expect.
This is because python keeps references to objects. When you say bb = aa,
you are really saying, "Take the reference to the list 'aa' and copy it to
'bb' so that when you use the variable bb you are just using the a copy of
the original reference to the *same* object."
Here's another gotcha:
s = []*100 #Thinking that you can initialize a hundred lists easily
s.append(1) #Add something to the first list
And, oh crap, it added a one to all of the lists.
More information about the Tutor