简体   繁体   English

将类方法作为参数传递给另一个类方法时出错

[英]error passing a class method as an argument to another class method

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. 我将三个参数传递给函数doSomething,但是,此异常不断出现。 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. 谢谢。

You've not passed three arguments; 您尚未传递三个参数; you passed two. 你通过了两个。 You need this: 你需要这个:

MC.attemptTenTimes(MC.doSomething,*('argument2','argument3'))

or this (equivalent): 或此(等效):

MC.attemptTenTimes(MC.doSomething,'argument2','argument3')

The attemptTenTimes function has the parameter *args , which collects positional arguments into a tuple referred to locally as args . attemptTenTimes函数具有参数*args ,该参数将位置参数收集到一个在本地称为args的元组中。 You're passing it the whole tuple as the only positional argument, so locally you have a variable named args that looks like ((MC,'argument2','argument3'),) . 您将整个元组作为唯一的位置参数传递给它,因此在本地有一个名为args的变量,看起来像((MC,'argument2','argument3'),) As a result, when you unpack it and pass it to your function, you're just passing the inner tuple. 结果,当您将其解压缩并将其传递给函数时,您只是在传递内部元组。

As an aside, you also shouldn't be unpacking args when you pass it to len , because that'll throw an error. 顺便说一句,当您将args传递给len ,也不应解压缩args,因为那样会引发错误。 You just want len(args) on line 12 up there. 您只希望第12行的len(args)在那里。

Alternately , you could change your attemptTenTimes function signature to this: 或者 ,您可以将tryTenTimes函数签名更改为此:

def attemptTenTimes(self,fun,args):

You could then pass the whole args tuple to it, as you were originally doing. 然后,您可以像最初那样将整个args元组传递给它。 I believe using *args is more standard, though, and personally I think it's clearer. 我相信使用*args更标准,但就我个人而言,它更清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM