object1
只是一个指向实例对象的标识符(或变量),对象没有名称。
>>> class A:
... def foo(self):
... print self
...
>>> a = A()
>>> b = a
>>> c = b
>>> a,b,c #all of them point to the same instance object
(<__main__.A instance at 0xb61ee8ec>, <__main__.A instance at 0xb61ee8ec>, <__main__.A instance at 0xb61ee8ec>)
a
, b
,c
只是允许我们访问同一个对象的引用,当一个对象有0个引用时,它会自动被垃圾回收。
一个快速的技巧是在创建实例时传递名称:
>>> class A:
... def __init__(self, name):
... self.name = name
...
>>> a = A('a')
>>> a.name
'a'
>>> foo = A('foo')
>>> foo.name
'foo'
>>> bar = foo # additional references to an object will still return the original name
>>> bar.name
'foo'