Originally Posted by
sh228
I've noticed an interesting behavior in python 2.6 with lists.
If you assign string 'string' to a, then run this code:
Code:
a='string'
b=a
b=''
print a
a will still equal 'string'. However, if a is a list:
Code:
a=['item1', 'item2']
b=a
del b[0]
print a
a will be just ['item2']. While this is rather annoying, it is fortunately easy to work around.
Thoughts?
In python, variables are just names for objects, not the objects themselves. If you're familiar with C or C++, you can think of every variable as a pointer to a generic Object type (ignoring the whole virtual mess). Here's what you're doing:
Code:
#a = "string"
#b = a
a -- "string"
b --/
# b = ""
a -- "string"
b -- ""
# now, a still refers to "string"
The second example:
Code:
# a = [item1, item2]
# b = a
a -- [item1, item2]
b --/
# del b[0]
a -- [item2]
b --/
# a still refers to the same, now changed, list
Bookmarks