[Python-ideas] Default arguments in Python - the return - running out of ideas but...
spir
denis.spir at free.fr
Fri May 15 18:05:39 CEST 2009
Le Fri, 15 May 2009 18:32:32 +0900,
"Stephen J. Turnbull" <stephen at xemacs.org> s'exprima ainsi:
> Chris Rebert writes:
>
> > +1 on throwing a ValueError for non-hash()-able (and thus probably
> > mutable) default argument values. It's by no means perfect since
> > objects are hash()-able by default using their ID, but it would at
> > least help in the frequent "well-behaved mutable container object"
> > cases.
>
> -1
>
> nonet_default_options = { 'dryrun': False, 'verbose': False }
> net_default_options = { 'dryrun': True, 'verbose': True }
>
> def command1(options=nonet_default_options):
> pass
>
> def command2(options=net_default_options):
> pass
>
> def command3(options=net_default_options):
> pass
Yop. The main issue is not that a default be mutable -- and the example above is perfect to show this -- but that it is changed in the func body, this change affects the default back and propagates to later calls.
/This/ could be used as criteria for a warning (or error), but probably it's much more costly to detect (parameter name on left side of "=").
Denis
------
la vita e estrany
More information about the Python-ideas
mailing list