I'm attempting to pass a class method as an argument to another class method. Below is an example...
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',))
The output is....
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).............
I am passing three arguments to the function doSomething, however, this exception keeps coming up. I've used functions as arguments to other functions before, but this is my first time doing it within the context of a class. Any help would be appreciated. Thanks.