dimanche 29 mars 2015

Get the name of the python 3.x method that was called inside of __call__

I'm trying to add a method to a class whenever it is called. For instance:



class X(callable):
def __init__(self):
pass

def __call__(self, *args, **kwargs):
method_called = self.get_method_name()

if method_called not in self.__dict__:
self.__dict__[method_called] = self.generic_function

super().__call__(self, *args, **kwargs)

def generic_function(self):
print("I'm So Generic")

def a(self):
print('a')

def get_method_name(self):
#get the name of the method trying to be called
method_name = ?????
return method_name

def main():
x = X()
#should create a method nonexistant_method that acts like generic_function
x.nonexistant_method()

main()


I realize that this might be an odd thing to want to do, but it's part of a tool that I'm building for some research. Is this possible? Or perhaps there is a better place to accomplish this than in __call__?


Thanks!






Aucun commentaire:

Enregistrer un commentaire