简体   繁体   English

为什么不能在Python中使用变量作为参数来调用此函数?

[英]Why can I not call this function using a variable as an argument in Python?

Example: 例:

def somerando(a,b,c,d):
    if not a+b+c+d == 9000:
        return (a+b+c+d)

somerando(1,2,3,4)

Returns: 10 返回: 10

but

randonumbs = [1,2,3,4]

somerando(randonumbs)

Gives the following error: 给出以下错误:

TypeError Traceback (most recent call last) in ----> 1 somerando(randonumbs) ----> 1 somerando(randonumbs)中的TypeError Traceback(最近一次呼叫最近)

TypeError: somerando() missing 3 required positional arguments: 'b', 'c', and 'd' TypeError:somerando()缺少3个必需的位置参数:“ b”,“ c”和“ d”

your function expects 4 arguments. 您的函数需要4个参数。 randonumbs = [1,2,3,4] is a list (of four items); randonumbs = [1,2,3,4]是一个列表(包含四个项目); that is one argument for your function. 这是您的函数的一个参数。

you could do this: 您可以这样做:

randonumbs = [1,2,3,4]
somerando(*randonumbs)

this usage of the asterisk ( * ) is discussed in this question or in PEP 3132 . 问题PEP 3132中讨论了星号( * )的用法。

You passed randonumbs as list, means this whole list is considered as first argument to the function somerando 您将randonumbs作为列表传递,这意味着整个列表都被视为somerando函数的第一个参数
You can use somerando(*randonumbs) . 您可以使用somerando(*randonumbs)

Here, * means pass as tuple & ** means pass as dictionary (key, value pair) if you use ** in function parameters/ arguments. 如果在函数参数/自变量中使用** ,则*表示作为元组传递, **表示作为字典 (键,值对)传递。


Thank you. 谢谢。

The single-asterisk form of *args can be used as a parameter to send a non-keyworded variable-length argument list to functions, like below * args的单星号形式可用作参数,以将非关键字的可变长度参数列表发送给函数,如下所示

randonumbs = [1,2,3,4]
somerando(*randonumbs)

The double asterisk form of **kwargs is used to pass a keyworded, variable-length argument dictionary to a function. ** kwargs的双星号形式用于将关键字的变长参数字典传递给函数。

randonumbs = {'a':1, 'b':2, 'c': 3, 'd': 4}
somerando(**randonumbs)

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

相关问题 Python:如何调用以另一个函数作为参数的函数? - Python: How can I call a function that has another function as argument? 我需要使用变量调用 python function - I need to call a python function using a variable 我可以在 Python 中使用变量名“type”作为函数参数吗? - Can I use the variable name “type” as function argument in Python? 我想使用python中的线程调用以日志文件名作为参数的函数 - I want to call a function with logfile name as argument using thread in python Python。 使用定义的函数后,我无法调用此函数正在运行的变量 - Python. After using defined function i can't call for variable this function was operating on python:为什么当我调用 function 时它显示未定义的变量? - python: why it says undefined variable when I call function? 如何在Python中调用带有存储为字符串的变量的函数 - How in Python can I call a function with a variable which is store as a string 我可以在 python 中调用 function 并命名为 arguments 作为变量吗? - Can I call function in python with named arguments as a variable? 如果在 if 语句中使用该变量,为什么不能覆盖外部函数的参数值? - Why can't I overwrite the value of an argument of an outer function if I use that variable in an if statement? 如何在PHP中调用Python函数传递变量参数 - How to call Python function in PHP passing variable argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM