The usage of *args and **kwargs in python is clear to me and there are many questions out there in SO (eg Use of *args and **kwargs and What does ** (double star/asterisk) and * (star/asterisk) do for parameters?).
But one thing I would like to understand is: why is it not possible to simultaneously define mandatory positional args, mandatory kwarg arguments and eventually still allow catching other args and kwargs as in cant_do_that
below?
def one_kwarg_is_mandatory(*, b, **kwargs):
print(b)
for key, value in kwargs.items():
print(key, value)
def one_pos_arg_and_one_kwarg_are_mandatory(a, *, b, **kwargs):
print(a, b)
for key, value in kwargs.items():
print(key, value)
# I wanted a mandatory arg (a) and possibly parse other args (*args),
# then a mandatory kwarg (b) and eventually other kwargs (**kwargs)
def cant_do_that(a, *args, *, b, **kwargs):
print(a, b)
print(args)
for key, value in kwargs.items():
print(key, value)
# not really interested on this because "b" must be a kwarg and hiding
# it under **kwargs would not be explicit enough for my customer (sometimes myself ;))
def could_do_this_but(a, b, *args, **kwargs):
print(a, b)
print(args)
print(kwargs)
Yes one could get rid of b
in the could_do_this_but
function's signature, perform (for instance) a kwargs.get("b", None)
at the top of the function and raise some appropriate error if found None
... but having "b" directly on the function signature would allow faster and more explicit code development employing the function down the road.