How to get a unique id for bound methods?

Oren Tirosh oren.tirosh at gmail.com
Tue Aug 23 02:39:16 EDT 2005


Russell E. Owen wrote:
> I have several situations in my code where I want a unique identifier
> for a method of some object (I think this is called a bound method). I
> want this id to be both unique to that method and also stable (so I can
> regenerate it later if necessary).

>>> def persistent_bound_method(m):
... 	return m.im_self.__dict__.setdefault(m.im_func.func_name, m)
...
>>> class A:
... 	def x(self):
...         return
...
>>> a=A()
>>> a.x is a.x
False
>>> persistent_bound_method(a.x) is persistent_bound_method(a.x)
True
>>>




More information about the Python-list mailing list