我试图将一个类方法作为参数传递给另一个类方法。下面是一个例子...
import time
class MyClass(object):
def doSomething(self,argument2,argument3):
print argument2,argument3
def attemptTenTimes(self,fun,*args):
attempt = 0
while True:
try:
print 'Number of arguments: %s' % len(*args)
print args
output = fun(*args)
return output
except Exception as e:
print 'Exception: %s' % e
attempt += 1
time.sleep(10)
if attempt >= 10: return
else: continue
MC = MyClass()
MC.attemptTenTimes(MC.doSomething,(MC,'argument2','argument3',))
输出是......
Number of arguments: 3
((<__main__.MyClass object at 0x7f7e6be4e390>, 'argument2', 'argument3'),)
Exception: doSomething() takes exactly 3 arguments (2 given)
Number of arguments: 3
((<__main__.MyClass object at 0x7f7e6be4e390>, 'argument2', 'argument3'),)
Exception: doSomething() takes exactly 3 arguments (2 given)
Number of arguments: 3
((<__main__.MyClass object at 0x7f7e6be4e390>, 'argument2', 'argument3'),)
Exception: doSomething() takes exactly 3 arguments (2 given).............
我将三个参数传递给函数 doSomething,但是,这个异常不断出现。我以前使用函数作为其他函数的参数,但这是我第一次在类的上下文中这样做。任何帮助,将不胜感激。谢谢。