I have :
class A: def a(): pass After typing in the python command line:
Aobj = A() aBoundMeth = getattr(Aobj, 'a') My goal is to get the name of the method that aBoundMeth object represents. Is it possible to do it? Thank you in advance.
Assuming that the name of the method is the string 'a' (in this case), You can use the __name__ attribute on the function object.
e.g.
>>> Aobj = A() >>> aBoundMeth = getattr(Aobj, 'a') >>> aBoundMeth.__name__ 'a' Note that this is the function name when it was created. You can make more references to the same function, but the name doesn't change. e.g.
>>> class A(object): ... def a(self): ... pass ... b = a ... >>> Aobj = A() >>> Aobj.a.__name__ 'a' >>> Aobj.b.__name__ 'a' dir gets it's its info from __dict__. If you actually look at a function object's __dict__. If you look at the function object's dict, it is empty -- So python must be doing some sort of special __getattr__ magic within the interpreter to give you the name.
getattrhere?aboundMeth(regardless of how it obtained its value), what is a/the name associated with the bound method object. UsingaBoundMeth = Aobj.awould not affect the answer.