Skip to content

Instantly share code, notes, and snippets.

@puzzlet
Created October 22, 2012 05:41
Show Gist options
  • Save puzzlet/3929884 to your computer and use it in GitHub Desktop.
Save puzzlet/3929884 to your computer and use it in GitHub Desktop.
never use id() with unbound function
$ python2.7 id_anomaly.py
old_func is not new_func
('old_func:', 41458576, <bound method classobj.member_func of <class __main__.A at 0x278bf58>>)
('new_func:', 41314096, <bound method classobj.member_func of <class __main__.A at 0x278bf58>>)
$ python3.2 id_anomaly.py
old_func is not new_func
old_func: 140100968595896 <bound method type.member_func of <class '__main__.A'>>
new_func: 140100968596184 <bound method type.member_func of <class '__main__.A'>>
class A:
@classmethod
def get_b(cls):
b = B()
b.register('func', cls.member_func)
b.register('func', cls.member_func)
return b
@classmethod
def member_func(cls):
pass
class B:
def __init__(self):
self.func_map = {}
# From flask.app.add_url_rule
def register(self, name, new_func):
old_func = self.func_map.get(name)
if old_func is not None and old_func is not new_func:
print("old_func is not new_func")
print("old_func:", id(old_func), old_func)
print("new_func:", id(new_func), new_func)
self.func_map[name] = new_func
A.get_b()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment