I hava a question about *args and **kwargs. I know that they are used when you do not know how many arguments will be passed to function. But can it be a substitute for some arguments that are actually required when I do not know what those are?
If there is a function:
def functionName(a, b):
...some code...
doSomethingUsing(a)
doSomethingUsing(b)
If I do not what arguments does the function take can I simply use functionName(*args, **kwargs) - or functionName(*args)? I noticed that some people tend to use it that way - but I am not sure if this is how * and ** work in python?
Calling function(*args)
is equivalent to function(arg[0], arg[1],...,arg[N])
.
If you don't know what arguments the function is expecting, just call it and look at the exception:
>>> def f(a,b,c):
... pass
...
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: f() missing 3 required positional arguments: 'a', 'b', and 'c'
Or you could use the interactive help
.
>>> help(f)
Help on function f in module __main__:
f(a, b, c)