welcome to the world of…

Looks like a small bulb used to indicate something unusual, like a malfunction.

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

Filed under: Uncategorized — Tags: , , , — admin @ 2009-02-24 18:47

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.

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.