I have a question regarding how to define and call a method with both optional arguments and with a *args parameter in Python. For example,
def test_func(arg1, optional_arg=None, *args):
...
And I want to be able to call the method with either of the following statements:
test_func("foo", arg1, arg2, ..., optional_arg="bar")
...
test_func("foo", arg1, arg2, ...)
# arg1, arg2 ... belongs to *args;
# optional_arg is unset (defaults to None)
Is this possible at all, or do I always have to set optional_arg to something when calling a method like this? I.e. I only want optional_arg to be set when I specifically write optional_arg=… when calling the function.
You should use **kwargs for that.
The only limitation is that the keyworded argument (optional_arg) must always be after keywordless arguments