fork download
  1. import inspect
  2.  
  3. def print_id(obj):
  4. print "{} => {}".format(id(obj), obj)
  5.  
  6. def getmember(obj, name):
  7. #members = dict(inspect.getmembers(obj))
  8. #return members[name]
  9. return [member
  10. for _name, member in inspect.getmembers(obj)
  11. if name == _name][0]
  12.  
  13. class Foo(object):
  14. def bar(self): pass
  15.  
  16. foo = Foo()
  17.  
  18. m1 = foo.bar
  19. m2 = getattr(foo, 'bar')
  20. m3 = getmember(foo, 'bar')
  21.  
  22. print_id(m1)
  23. print_id(m2)
  24. print_id(m3)
  25.  
Success #stdin #stdout 0.14s 11296KB
stdin
Standard input is empty
stdout
3073722804 => <bound method Foo.bar of <__main__.Foo object at 0xb73637cc>>
3073563300 => <bound method Foo.bar of <__main__.Foo object at 0xb73637cc>>
3072858948 => <bound method Foo.bar of <__main__.Foo object at 0xb73637cc>>