Just a small solution that I came up with using an additional function can be improved, of course:
defaultargs.py:
def doInit(var, default_value,condition): if condition: var = default_value return var def func(a=None, b=None, c=None): a = doInit(a,5,(a is None or not isinstance(a,int))) b = doInit(b,10.0,(a is None or not isinstance(a,float))) c = doInit(c,"whatever",(a is None or not isinstance(c, str))) print a print b print c if __name__ == "__main__": func(10) func(None,12341.12) func("foo",None,"whowho")
exit:
10 10.0 whatever 5 10.0 whatever 5 10.0 whowho
I like your question. :)
Edit: If you do not care about the type of variables, do not use isinstance ().
user945967
source share