I want to code the apply_method function:
class A:
def calc(self, num):
return num
def apply_method(class_method, *args, **kwargs):
# Identify class module A from class_method: Is this possible?
# Import class module A
# Create an instance of the class A, say obj
# return obj.class_method(*args, **kwargs)
pass
class_method = A.calc
input_num = 5
return apply_method(class_method, 5)
It is guaranteed that the module for class A is available to be imported wherever apply_method gets called.
Is all the information necessary to create apply_method available inside class_method?
The reason I need this: apply_method is a callback. The callback only contains a function and parameters. The callback needs to be made across hundreds of different classes. In the absence of this generic method, I will need to handle callbacks very specifically for each of the classes.
Play around with some of the __ methods in the interpreter. Everything you need should be available in one form or another: