You can receive all the arguments that you do not know how many will come.
python
def test(*args, **kwargs):
print(args)
print(kwargs)
test(1, 2, 'hoge')
output
(1, 2, 'hoge')
{}
** Enter kwargs with dict ().
python
def test(*args, **kwargs):
print(args)
print(kwargs)
test(1, 2, 3, 4, 5, col=4, row=5)
output
(1, 2, 3, 4, 5)
{'col': 4, 'row': 5}
It will be an empty tuple () and an empty dict ().
python
def test(*args, **kwargs):
print(args)
print(kwargs)
test()
output
()
{}
You can pass the received argument as it is
python
def func1(a, b, c=2):
d = a + b * c
return d
def func2(*args, **kwargs):
d = func1(*args, **kwargs)
return d
print(func2(1, 2))
print(func2(1, 2, c=5))
output
5
11
In fact, if the number of * is the same, the argument name can be changed freely. You can use * a, * hoge instead of * args, and ** b, ** fuga etc. instead of ** kwargs.
python
def test(*a, **b):
print(a)
print(b)
test(1, 2, 3, 4, 5, col=4, row=5)
output
(1, 2, 3, 4, 5)
{'col': 4, 'row': 5}
Let's try!
Recommended Posts