JavaScript style
Suppose I have a function takes two objects as optional parameters input, and some of the args in the objects has default value, in JavaScript, I may implement it like this:
function func(setting1, setting2){
arga = setting1.a || 1;
argb = setting2.b || 2;
argc = setting2.c;
...
}
Python implementation
There's some ideas comes to my mind, but i think none of them is actually "good".
Here's the first one:
params = {"arga":2, "argb":3}
def func(argc, arga=1, argb=2):
...
func(**params)
The question is, this type of implementation cannot support the semantic i want, that is, the setting1 and setting2 need two be separated since they serve for different sub-procedures. Also, It need to manually choose the default parameters's position carefully.
Here's the second one:
def func(setting1, setting2):
try:
arga = setting1.a
except KeyError:
arga = 1
...
I think it's quite ugly.
Maybe this is the best one:
def func(setting1, setting2):
arga = setting1.get("a") or 1
argb = setting1.get("b") or 2
...
Is there any good way to implement this in python?