I suppose I understand how to use them individually in functions like ...
f(*args) or f(**kargs)
But what if a function accepts both as arguments? Ex: f(*args, **kargs)
I suppose I understand how to use them individually in functions like ...
f(*args) or f(**kargs)
But what if a function accepts both as arguments? Ex: f(*args, **kargs)
Yes, it works
def f(*args, **kwargs):
pass
If you call function like this f(1, 3, "foo", [1, 2, 10], a=1, apple=33)
, then in function args
will be (1, 3, "foo", [1, 2, 10])
, kwargs
will be{'a': 1, 'apple': 33}
.
It will work too
def f(a, b, foo, *args, **kwargs):
pass
But if we call this function with the same arguments, a
will be 1
, b
will be 3
, foo
will be "foo"
, args
will be ([1, 2, 10])
, kwargs
will be the same.
*args
gets positional arguments; **kwargs
gets named arguments. For instance:
f(1, 2, 3, a=1, b=2, c=3)
would pass
args = (1, 2, 3)
kwargs = {a: 1, b: 2, c: 3}