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_funcid()決して変更されるべきではない元の関数への参照。

しかし、上記の質問では、この問題はさらに簡潔に扱われています。

于 2013-03-20T11:06:01.990 に答える