--What are the meanings of *
and **
defined in function parameters and how to use them?
I would like to organize it here so that I can remember it quickly even if time passes.
*
and **
are attached to parameters that receive variadic arguments.I saw the code that says * args
and ** kwargs
in the function parameters.
This is described in a parameter that receives variadic arguments, and the number of arguments passed to the parameter when calling a function can be any number instead of fixed.
Variadic arguments are those that allow you to specify any number of arguments where you can usually specify only one argument.
Example
def kansu(*args):
print(f'{args}Received!')
kansu('Apple', 'Banana', 100)
# ('Apple', 'Banana', 100)Received!
As for the parameter, 3 arguments are passed for only one ʻargs`, but it works without error.
In a familiar example, the print () function
and the .format () method
also use variadic arguments.
You can pass as many arguments as you like
print('a','b','c') # a b c
Also, the names ʻargs and
kwargs` are customarily used, so you can use any name you like.
def x(*z):
y = []
for i in z:
y.append(i*10)
print(y)
x(1,2,3,4,5,6,7,8)
# [10, 20, 30, 40, 50, 60, 70, 80]
*
is one and when it is twoThere are two types of variable-length arguments, each of which has a different way of passing and receiving arguments.
* args
Receive any number of arguments as a tuple (if you don't pass any arguments, it will be an empty tuple)
Pass as a positional argument* kwargs
Receive any number of arguments of the form keyword = value
in dictionary type ( key value
)
(If you don't pass any arguments, it will be an empty dictionary)
Pass as keyword argumentposition
argumentsBy adding *
to the argument, the parameter will receive a variable length position argument.
That is, it receives any number of arguments.
The usage of the function depends on where the variable-length positional argument is defined in the parameter of the function.
1.The last parameter is variable length
def myfunction(x, y, *z):
print(f'{x} - {y} - {z}')
myfunction(2, 3, 5, 6, 7) # 2 - 3 - (5, 6, 7)
The first and second arguments correspond to x
and y
, respectively.
Subsequent arguments correspond to z
and are stored as tuples.
2.Variable length parameters on the way
def myfunction(x, *y, z):
print(f'{x} - {y} - {z}')
myfunction(2, 3, 5, 6, 7) # TypeError: myfunction() missing 1 required keyword-only argument: 'z'
It throws an error because no arguments have been passed to the parameter z
. In this case, z
is a keyword-only argument, so you must set a default value or use it as a keyword argument when calling.
2-1.Set default value
def myfunction(x, *y, z=0):
print(f'{x} - {y} - {z}')
myfunction(2, 3, 5, 6, 7) # 2 - (3, 5, 6, 7) - 0
2-2.Use as a keyword argument
def myfunction(x, *y, z):
print(f'{x} - {y} - {z}')
myfunction(2, 3, 5, 6, 7, z=0) # 2 - (3, 5, 6, 7) - 0
keyword
argumentsBy adding **
to the argument, the parameter receives a variable length keyword argument.
That is, it receives an arbitrary number of keywords = values
, and the keywords and arguments correspond to the dictionary-type key
and value
, respectively.
How to use the function depends on where you define **
in the parameters of the function.
1.Make the last parameter a variable length keyword argument
def myfunction(param1, param2, **param):
print(f'{param1} | {param2} | {param}')
myfunction(2, 3, param3=4, param4=5, param5=6) # 2 | 3 | {'param3': 4, 'param4': 5, 'param5': 6}
The value obtained by the variable length keyword argument becomes a dictionary type.
2.Make the parameter in the middle a variable-length keyword argument
def myfunction(param1, **param, param2):
print(f'{param1} | {param2} | {param}')
The result is a syntax error It is not allowed to define variable length keyword arguments in the middle.
Normal parameters after parameters that receive variable-length positional arguments are keyword-only arguments.
def Test(a, *b, c): #c is for keywords only
pass
There can be no regular parameters after the parameters that receive variable-length keyword arguments.
05-2.Repost
def myfunction(param1, **param, param2):
print(f'{param1} | {param2} | {param}')
Therefore, when used together, the order must be as follows.
1.A parameter that receives both positional and keyword arguments
2.Parameters that receive variable-length positional arguments
3.Parameters that receive keyword-only arguments
4.Parameters that receive variable-length keyword arguments
Recommended Posts