In Python, when creating a function that accepts arbitrary arguments, prefix the arguments with \ * or \ * \ *.
From the function side, if you add \ *, it becomes Tuple, and if you add \ * \ *, it will be received in Dictionary. From the caller's point of view, when passing by Tuple, there is no Keyword, and when passing by Dictionary, there is Keyword.
def func1(*param1):
print(param1)
def func2(**param1):
print(param1)
In [3]: func1(1,2,3,4,5)
(1, 2, 3, 4, 5)
In [4]: func2(a=1,b=2,c=3,d=4,e=5)
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4}
Unpacking Argument
When you want to hand over the contents of tuple separately
The results of func1 (* t1)
and func1 (0,1,2)
are the same.
For Dictionary, use **.
In [13]: t1 = (0,1,2)
In [14]: func1(*t1)
(0, 1, 2)
In [15]: func1(0,1,2)
(0, 1, 2)
In [16]: func1(t1)
((0, 1, 2),)
Details can be found here [https://docs.python.org/dev/tutorial/controlflow.html#keyword-arguments).
Recommended Posts