[Python-ideas] Default arguments in Python - the return

spir denis.spir at free.fr
Wed May 13 12:25:23 CEST 2009
Le Wed, 13 May 2009 16:34:02 +0900,
"Stephen J. Turnbull" <stephen at xemacs.org> s'exprima ainsi:

> But those also have a convenient
> literal syntax that makes it clear that the object is constructed at
> function call time: "myfunc([])" and "myfunc({})".

I totaly agree with you, here. And as you say "explicit, etc..."

Then, the argument must not be 'defaulted' at all in the function def. This is a semantic change and a pity when the argument has an "obvious & natural" default value.
While we're at removing default args and requiring them to be explicit instead, why have default args at all in python?
Especially, why should immutable defaults be OK, and mutable ones be wrong and replaced with explicit value at call time? I mean that this distinction makes no sense conceptually: the user just wants a default; this is rather python internal soup detail raising an issue.

I would rather require the contrary, namely that static vars should not be messed up with default args. This is not only confusion in code, but also design flaw. here's how I see it various possibilities (with some amount of exageration ;-):

def f(arg, lst=[]):
   # !!! lst is no default arg, !!!
   # !!! it's a static var instead !!!
   # !!! that will be updated in code !!!
   <do things>

def f(arg):
   <do things>
f.lst = []       # init static var

 or

def f(arg):
   # init static var
   try:
      assert f.lst
   except AttributeError:
      f.lst = []
   <do things>

Denis
------
la vita e estrany



More information about the Python-ideas mailing list