3

这是Python 的 docs re build-in function的摘录id()

== 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
4

1 回答 1

0

a.f转换为“原始”函数对象f.__get__(a, A)在哪里。f这样,函数会生成一个包装器,并且每次调用都会生成这个包装器。

a.f.im_func引用回原来的函数,它id()永远不应该改变。

但是在上面提到的问题中,这个问题的处理更加简洁。

于 2013-03-20T11:06:01.990 回答