I have been looking into the use of *args
and **kwargs
and i have consulted
a number of relevant posts, which provide quite a few interesting examples:
What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
How to expand a list to function arguments in Python
Converting Python dict to kwargs?
However, the above threads do not provide a clear answer to the question of what is the best way of flexibly using the same (keyword) arguments and their default values in multiple functions? And by flexibly i mean being able to also define additional (keyword) arguments in each function on a case by case basis.
Essentially, i would like to avoid manually defining the same arguments in each function over and over again, instead only focusing on the additional ones that are required in each function.
For example, instead of:
def testfunction1(voltage=0, state="bleedin", status=0):
...do something...
def testfunction2(voltage=0, state="bleedin", status=0, action="VOOM"):
...do something else...
(note: status
could be anything like a list or a tuple or an array.)
I would like something along the following lines:
d = {"voltage": 0, "state": "bleedin", status}
def testfunction1(**d):
...do something...
def testfunction2(**d, action="VOOM"):
...do something else...
I could then call each function like:
testfunction1(voltage=5, state="healthy")
or specify the arguments directly like:
testfunction1((5, "healthy", statuslist)
Hopefully this is clear but i would happy to clarify further if necessary.