简体   繁体   English

将接受多个 arguments 作为参数的 function 传递给 function

[英]Pass a function that accepts multiple arguments as an argument to a function

I'm studying how to pass a function as an argument.我正在研究如何将 function 作为参数传递。 I've encountered a problem where I tried to invoke a passed-in function which accepts multiple arguments.我遇到了一个问题,我尝试调用传入的 function,它接受多个 arguments。

Code:代码:

    def add(*numbers):
        total = 0
        for number in numbers:
            total += number
        return total

    def apply(func, value):
        return func(value)


    print(apply(add, (1,2,3)))

Output: Output:

    Traceback (most recent call last):
      File "D:\PythonPrac\test.py", line 13, in <module>
        print(apply(add, (1,2,3)))
      File "D:\PythonPrac\test.py", line 9, in apply
        return func(value)
      File "D:\PythonPrac\test.py", line 4, in add
        total += number
    TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'

Thanks again!再次感谢!

you are summing up an integer to a tuple:您将 integer 总结为一个元组:

0 + (1,2,3)

so when you call it you have to do this:所以当你调用它时,你必须这样做:

add(1,2,3)

or you have to change the add function in this way:或者您必须以这种方式更改添加 function:

def add(numbers):
        total = 0
        for number in numbers:
            total += number
        return total

so in this case you iterate trough number that is a tuple: (1,2,3) and not a list of tuple like before: [(1,2,3)]因此,在这种情况下,您迭代作为元组的槽数: (1,2,3)而不是像以前那样的元组列表: [(1,2,3)]

then, it might be a stupid question because you can have your reasons (and maybe it wil be better to you to explain them) but why do you do:那么,这可能是一个愚蠢的问题,因为你可以有你的理由(也许你最好解释一下)但是你为什么这样做:

apply(add, (1,2,3))

instead of:代替:

add((1,2,3)) or add(1,2,3) (it depends if you use your or my version of the add function) add((1,2,3))add(1,2,3) (这取决于您是否使用您或我的 add 函数版本)

it appear like a nonsense thing that you use a function that ONLY calls another function instead of calling it directly您使用 function 似乎是胡说八道,它只调用另一个 function 而不是直接调用它

finally if you want to do it in your way you have to do:最后,如果您想以自己的方式进行操作,则必须这样做:

def add(*numbers):
        total = 0
        for number in numbers[0]:
            total += number
        return total

Have removed asterisk * from numbers in add Function已从添加 Function 中的数字中删除星号 *

       def add(numbers):
           total = 0
           for number in numbers:
              total += number
           return total

       def apply(func, value):
          return func(value)  

Add an asterisk before values on line 8.在第 8 行的值之前添加一个星号。

def add(*numbers):
    total = 0
    for number in numbers:
        total += number
    return total

def apply(func, value):
    return func(*value)


print(apply(add, (1,2,3)))

Explenation: This unpacks the tuple as arguments, so the evalution will be roughly like this:说明:这会将元组解包为 arguments,因此评估大致如下:

apply(add, (1, 2, 3))
add(*(1, 2, 3))
add(1, 2, 3)

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

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