Python学习之引用
When you create an object and assign it to a variable, the variable only refers to
the object and does not represent the object itself!
当你创建一个对象并给它赋一个变量的时候,这个变量仅仅引用那个对象,而不
是表示这个对象本身!也就是说,变量名指向你计算机中存储那个对象的内存。这被
称作名称到对象的绑定。
>>> x=[1,2,3]
>>> y=x
>>> y[0]=100 #改变了x,和y共同指向的那个对象的值。(注意,list是可以被改变的数据类型!tuple和str就不支持这样的操作)
>>> x
[100, 2, 3]
>>> y
[100, 2, 3]
>>> del y[0]
>>> y
[2, 3]
>>> x=[1,2,3]
>>> y=x
>>> del y #不是删除y所指向的对象!
>>> x
[1, 2, 3] # del y只是删除了上述对象的一个引用而已。
>>> y
Traceback (most recent call last):
File "<pyshell#79>", line 1, in <module>
y
NameError: name 'y' is not defined
the object and does not represent the object itself!
当你创建一个对象并给它赋一个变量的时候,这个变量仅仅引用那个对象,而不
是表示这个对象本身!也就是说,变量名指向你计算机中存储那个对象的内存。这被
称作名称到对象的绑定。
>>> x=[1,2,3]
>>> y=x
>>> y[0]=100 #改变了x,和y共同指向的那个对象的值。(注意,list是可以被改变的数据类型!tuple和str就不支持这样的操作)
>>> x
[100, 2, 3]
>>> y
[100, 2, 3]
>>> del y[0]
>>> y
[2, 3]
>>> x=[1,2,3]
>>> y=x
>>> del y #不是删除y所指向的对象!
>>> x
[1, 2, 3] # del y只是删除了上述对象的一个引用而已。
>>> y
Traceback (most recent call last):
File "<pyshell#79>", line 1, in <module>
y
NameError: name 'y' is not defined