我想调用命名相同的不同类的方法,目的是能够在不同的django模型上调用save()或delete()(当然所有django模型都有相同的方法)。我试过了:
class X(object):
def __init__(self, value):
self.value = value
def double(self):
self.value = self.value * 2
def triple(self):
self.value = self.value * 3
def testcallback(obj, callback):
return obj.callback
并以任何方式调用它都不起作用
In [26]: y = testcallback(x, triple)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-26-09fc8db1d48b> in <module>()
----> 1 y = testcallback(x, triple)
NameError: name 'triple' is not defined
In [31]: y = testcallback(x, triple())
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-31-5a7a2dd91850> in <module>()
----> 1 y = testcallback(x, triple())
NameError: name 'triple' is not defined
我不断对不同的相关模型进行相同的检查,然后根据它们来自哪种形式保存或删除。如何将任意对象和方法名称发送到函数中,然后调用在python中选择的对象上选择的方法?谢谢