If you cannot change the way you call your methods, you can use the __getattribute__ magic method (the methods are too remembered for attributes!), You just need to be careful to check the type of the attributes so that you don't type “I:” every time you want to get access to any sting or int attributes that may be present:
import types class Human(object): def __getattribute__(self, attr): method = object.__getattribute__(self, attr) if not method: raise Exception("Method %s not implemented" % attr) if type(method) == types.MethodType: print "I am:" return method def eat(self): print "eating" def sleep(self): print "sleeping" def throne(self): print "on the throne" John = Human() John.eat() John.sleep() John.throne()
Outputs:
I am: eating I am: sleeping I am: on the throne
source share