[Python-ideas] Default arguments in Python - the return - running out of ideas but...
spir
denis.spir at free.fr
Thu May 14 13:44:07 CEST 2009
Le Wed, 13 May 2009 20:10:42 -0400,
George Sakkis <george.sakkis at gmail.com> s'exprima ainsi:
> Subproposal (2): If subproposal (1) is accepted, we could get for free
> (in terms of syntax at least) dynamic args depending on previous ones.
> That is,
>
> def myfunc(a, b, *m=(a+b)/2):
>
> would mean
>
> def myfunc(a, b, *m = lambda a,b: (a+b)/2):
>
> with the lambda being passed the values of a and b at runtime.
While I understand the intent, this seems complicated to me. I find clearer to express m in the func body; using a sentinel if m is a real default arg (meaning it could possibly be passed by the user).
UNDEF = object()
def myfunc(a, b, m=UNDEF):
if m is UNDEF:
m = (a+b)/2)
Generally speaking, I find ok the need of sentinels for clarifying rare and non-obvious cases such as runtime-changing default values:
def somefunc(arg, m=UNDEF):
if m is UNDEF:
m = runtimeDefaultVal()
While I do not find ok the need of a sentinel to avoid the common gotcha of a default value beeing "back-updated" when the corresponding local var is changed in the func body:
def otherfunc(arg, l=UNDEF):
if l is UNDEF:
l = []
<possibly update l>
Denis
------
la vita e estrany
More information about the Python-ideas
mailing list