2013-03-20 22 views
3

下面是一个excerpt from Python's docs re id() build-in functionPython的ID(OBJ)改变的ad-hoc

== id(object) == 
Return the “identity” of an object. This is an integer (or long integer) which is 
guaranteed to be unique and constant for this object during its lifetime. 
Two objects with non-overlapping lifetimes may have the same id() value. 

CPython implementation detail: This is the address of the object in memory. 

那么......怎么就在下面的例子中变化?

>>> class A(object): 
... def f(self): 
...  return True 
... 
>>> a=A() 
>>> id(a.f) 
Out[21]: 62105784L 
>>> id(a.f) 
Out[22]: 62105784L 
>>> b=[] 
>>> b.append(a.f) 
>>> id(a.f) 
Out[25]: 50048528L 

回答

0

a.f转化为类似f.__get__(a, A)其中f是 “原始的” 函数对象。这样,函数就会生成一个包装器,并且这个包装器会在每次调用时生成。

a.f.im_func引用回原始函数,其原始函数不应更改。

但是在question referenced above这个问题更加简洁。