Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does id() of an unbound method in Python 2 change for every access

Python 2.6.5 (r265:79063, Oct 1 2012, 22:07:21) [GCC 4.4.3]

>>> class myclass:
...     def func(self):
...             pass

>>> dd = myclass.func
>>> ee = myclass.func
>>> cc = myclass.func
>>> ff = myclass.func
>>> ss = myclass.func
>>> uu = myclass.func
>>> pp = myclass.func
>>> 
>>> 
>>> id(dd) ; id(cc) ; id(ee) ; id(ff) ; id(ss) ; id(uu) ; id(pp)
3074535252L
3074534772L
3074522444L
3074531732L
3074497588L
3073003604L
3073003724L

Why is the ID of the unbound method different each time?

Shouldn't it be same ?

like image 924
Ankur Agarwal Avatar asked Aug 16 '13 01:08

Ankur Agarwal


1 Answers

This is because the methods on a class (old or new) work really like attributes with the descriptor __get__ method; On python 2 the code

foo = FooClass.bar_method

is analogous to

import types
foo = types.MethodType(FooClass.__dict__['bar_method'], None, FooClass)

It will create a new instance of instancemethod(bar_method, None, FooClass) on each access. The original function is available as FooClass.bar_method.im_func and the class instance in foo.im_class. The type for both bound and unbound methods is the same instancemethod; if the im_self member is None, the instancemethod instance has the repr <unbound method ...>, whereas if im_self member is not None, the repr is <bound method...>

Python 3 is different. Unbound methods have a repr <function x.f at 0x7fd419cf69e0> and the id is always the same, that is they are just general functions. In Python 3 you can pass anything for self of an unbound undecorated method, even None, it is just a function with a dot in its name.