If you want a function to receive arbitrarily multiple parameters, you can use variable-length parameters.
There are two variable length parameters in python custom functions.
*args
: The first is *XXX
. When passing in additional parameters, you can pass in the parameter value directly without specifying the parameter name. *kargs
: The second type is **XXX
This type returns a dictionary. You need to specify the parameter name when you pass it in.Added an asterisk *
Variable length parameters will be imported in the form of tuples, storing all unnamed variable parameters.
The parameters with two asterisks **
will be imported in the form of a dictionary to store the named variable parameters.
#coding=utf-8
#The first one is the tuple form, the second is the dictionary form
def sun(*a, **b):
print(a)
print(b)
sun(1, 55258, x=25412, y=5123512)
output
(1,55258)
{'x':25412,'y':5123512}
A simple example of variable-length parameters, the first is a normal parameter, the second is a variable-length parameter, we can print out in turn, the variable-length parameter is in the form of a tuple, such as b represents (55258,25412,5123512)
, and As a common parameter, a can only represent the first 1
.
#coding=utf-8
#Indefinite length parameter simple example, the first is a normal parameter, the second is an indefinite length parameter
def sun(a, *b):
print(a)
print(b)
sun(1,55258,25412,5123512)
output:
1
(55258, 25412, 5123512)
When a
, *b
, **c
appear at the same time, as follows:
#coding=utf-8
#Use variable length parameters to pass fixed values, note that b and c can be omitted, and a cannot be omitted
def fuzhi(a, *b, **c):
print(a)
print(b)
print(c)
fuzhi(853521, 65134, 635263, 45563, 365, x=99, y=999)
output:
853521
(65134,635263,45563,365)
{'x':99, 'y':999}
a common parameter a
cannot be omitted, if we omit a
#coding=utf-8
#Use variable length parameters to pass fixed values, note that b and c can be omitted, and a cannot be omitted
def fuzhi(a, *b, **c):
print(a)
print(b)
print(c)
fuzhi()
error prompt:
TypeError: fuzhi() missing 1 required positional argument: 'a'
When we use variable length parameters to accumulate:
#coding=utf-8
#Accumulate with variable length parameters
print(1, 2, 3, 4, 5)
def sum(*nums):
"""
Find the sum of multiple numbers
:param list_one: receives the variable length parameter of the number, will assemble the parameter into a tuple, and assign it to the variable length parameter
:return: return the sum of all numbers
"""
result = 0
for num in nums:
if isinstance( num, (int,float)):
result = result + num
return result
sum()
output:
1 2 3 4 5
6
When we write the ordinary parameter in the variable length parameter and write it in the back
#coding=utf-8
def JayChou(a, *b, c):
print(a)
print(b)
print(c)
JayChou(1, 555, 5768, 55451)
operation result
The following error will appear, so the position cannot be changed at will
TypeError: JayChou() missing 1 required keyword-only argument: 'c'
If no parameters are specified when the function is called, it is an empty tuple. We can also not pass unnamed variables to the function
#coding=utf-8
# If no parameters are specified in the function call, it is an empty tuple. We can also not pass unnamed variables to the function.
def void_tuple(a, *tuplela):
print(a)
for b in tuplela:
print(b)
return
print(1)
print(2, 3, 4, 5)
output:
1
2 3 4 5