2
识别类模块我要编码的apply_method功能:从类功能
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)
这是保证A类的模块可被导入的地方apply_method被调用。
是否需要在class_method
内部创建apply_method
?
我需要这个原因:apply_method是一个回调。回调只包含一个函数和参数。回调需要在数百个不同的类中进行。在没有这种通用方法的情况下,我需要为每个类特别处理回调。
您需要什么? –
什么是“班级模块”? – geoffspear
它取决于版本,在2.x中它应该是可能的 - 但是如果我在任何代码中看到这个我应该工作,你可以肯定我会得到作者的地址,并发挥谚语精神病患者..真的,这是一个可怕的想法。将类和方法名称作为一个字符串传递,或者只是将它们委派给一些神奇的效用函数。 – delnan