Quantcast
Viewing latest article 2
Browse Latest Browse All 6

Python’s built-in id() and class methods

I found a feature of the built-in function id() that is a little bit unexpected (Python 2.5).

class A(object):
    def f(self):
        pass
 
    def g(self):
        pass
 
print A.f, id(A.f), A.f.im_func
print A.g, id(A.g), A.g.im_func

output:

<unbound method A.f> 3084518300 <function f at 0xb7d94ae4>
<unbound method A.g> 3084518300 <function g at 0xb7d94764>

As you can see you can’t use the id() function with the method itself because it doesn’t return the desired unique number. Why? Some explanation can be found here. So you must destinguish between methods and functions implementing those methods. It looks like methods are in its essence objects with a very short lifetime.


Viewing latest article 2
Browse Latest Browse All 6

Trending Articles