Python中的 *args 和 **kwargs
在Python中,我们可以使用特殊符号将可变数量的参数传递给函数。有两个特殊符号:
用于传递参数的特殊符号:-
1.)*args(非关键字参数)
2.)**kwargs(关键字参数)
Note: “We use the “wildcard” or “*” notation like this – *args OR **kwargs – as our function’s argument when we have doubts about the number of arguments we should pass in a function.”
1.) *args
Python函数定义中的特殊语法*args用于将可变数量的参数传递给函数。它用于传递非关键字、可变长度的参数列表。
- 语法是使用符号 * 来接受可变数量的参数;按照惯例,它通常与 args 一词一起使用。
- *args允许您做的是接受比您之前定义的形式参数数量更多的参数。使用*args ,可以将任意数量的额外参数附加到您当前的形式参数(包括零个额外参数)。
- 例如:我们想要创建一个乘法函数,它接受任意数量的参数并且能够将它们全部相乘。可以使用 *args 来完成。
- 使用 *,我们与 * 关联的变量成为可迭代的,这意味着你可以做一些事情,比如迭代它,运行一些高阶函数,如 map 和 filter 等。
python3
# Python program to illustrate
# *args for variable number of arguments
def myFun(*argv):
for arg in argv:
print (arg)
myFun('Hello', 'Welcome', 'to', 'GeeksforGeeks')
Python3
# Python program to illustrate
# *args with first extra argument
def myFun(arg1, *argv):
print ("First argument :", arg1)
for arg in argv:
print("Next argument through *argv :", arg)
myFun('Hello', 'Welcome', 'to', 'GeeksforGeeks')
python
# Python program to illustrate
# *kwargs for variable number of keyword arguments
def myFun(**kwargs):
for key, value in kwargs.items():
print ("%s == %s" %(key, value))
# Driver code
myFun(first ='Geeks', mid ='for', last='Geeks')
python
# Python program to illustrate **kwargs for
# variable number of keyword arguments with
# one extra argument.
def myFun(arg1, **kwargs):
for key, value in kwargs.items():
print ("%s == %s" %(key, value))
# Driver code
myFun("Hi", first ='Geeks', mid ='for', last='Geeks')
python3
def myFun(arg1, arg2, arg3):
print("arg1:", arg1)
print("arg2:", arg2)
print("arg3:", arg3)
# Now we can use *args or **kwargs to
# pass arguments to this function :
args = ("Geeks", "for", "Geeks")
myFun(*args)
kwargs = {"arg1" : "Geeks", "arg2" : "for", "arg3" : "Geeks"}
myFun(**kwargs)
python3
def myFun(*args,**kwargs):
print("args: ", args)
print("kwargs: ", kwargs)
# Now we can use both *args ,**kwargs
# to pass arguments to this function :
myFun('geeks','for','geeks',first="Geeks",mid="for",last="Geeks")
输出:
Hello
Welcome
to
GeeksforGeeks
Python3
# Python program to illustrate
# *args with first extra argument
def myFun(arg1, *argv):
print ("First argument :", arg1)
for arg in argv:
print("Next argument through *argv :", arg)
myFun('Hello', 'Welcome', 'to', 'GeeksforGeeks')
输出:
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
2.)**kwargs
Python函数定义中的特殊语法**kwargs用于传递带关键字的可变长度参数列表。我们使用带有双星的名称kwargs 。原因是双星允许我们传递关键字参数(以及任意数量的参数)。
- 关键字参数是您在将变量传递给函数时为其提供名称的地方。
- 可以将kwargs视为一本字典,它将每个关键字映射到我们旁边传递的值。这就是为什么当我们遍历kwargs时,它们的打印顺序似乎没有任何顺序。
**kwargs 的用法示例:
Python
# Python program to illustrate
# *kwargs for variable number of keyword arguments
def myFun(**kwargs):
for key, value in kwargs.items():
print ("%s == %s" %(key, value))
# Driver code
myFun(first ='Geeks', mid ='for', last='Geeks')
输出:
last == Geeks
mid == for
first == Geeks
Python
# Python program to illustrate **kwargs for
# variable number of keyword arguments with
# one extra argument.
def myFun(arg1, **kwargs):
for key, value in kwargs.items():
print ("%s == %s" %(key, value))
# Driver code
myFun("Hi", first ='Geeks', mid ='for', last='Geeks')
输出:
last == Geeks
mid == for
first == Geeks
使用 *args 和 **kwargs 调用函数
例子:
蟒蛇3
def myFun(arg1, arg2, arg3):
print("arg1:", arg1)
print("arg2:", arg2)
print("arg3:", arg3)
# Now we can use *args or **kwargs to
# pass arguments to this function :
args = ("Geeks", "for", "Geeks")
myFun(*args)
kwargs = {"arg1" : "Geeks", "arg2" : "for", "arg3" : "Geeks"}
myFun(**kwargs)
输出:
arg1: Geeks
arg2: for
arg3: Geeks
arg1: Geeks
arg2: for
arg3: Geeks
在同一行中使用 *args 和 **kwargs 来调用函数
例子:
蟒蛇3
def myFun(*args,**kwargs):
print("args: ", args)
print("kwargs: ", kwargs)
# Now we can use both *args ,**kwargs
# to pass arguments to this function :
myFun('geeks','for','geeks',first="Geeks",mid="for",last="Geeks")
输出:
args: ('geeks', 'for', 'geeks')
kwargs {'first': 'Geeks', 'mid': 'for', 'last': 'Geeks'}