How to get a unique id for bound methods?

Paolino paolo_veronelli at tiscali.it
Fri Aug 19 19:10:26 EDT 2005


Russell E. Owen wrote:

> The "hash" function looks promising -- it prints out consistent values 
> if I use it instead of "id" in the code above. Is it stable and unique? 
> The documentation talks about "objects" again, which given the behavior 
> of id makes me pretty nervous.
> 
I dont know how the hash of a bound method is calculated,but as the 
function of the method is a stable and referenced object and as 
instances lives are in your hands,then an id(self)^id(self.meth.im_func) 
should be a chance for that 'hash' function.

def methodId(boundMethod):
   return id(boundMethod.im_self)^id(boundMethod.im_func)

class cls(object):
   def __init__(self):
     print methodId(self.meth1)
     print methodId(self.meth2)
   def meth1(self):
     pass
   def meth2(self):
     pass

c = cls()
print methodId(c.meth1)
print methodId(c.meth2)

I think this is giving what you expected.

Regards Paolino

	

	
		
___________________________________ 
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB 
http://mail.yahoo.it



More information about the Python-list mailing list