[Python-ideas] Default arguments in Python - the return - running out of ideas but...
Stephen J. Turnbull
stephen at xemacs.org
Fri May 15 11:32:32 CEST 2009
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
>>> from mystuff import command1, command2, net_default_options
>>> command1()
>>> command2()
>>> command3()
>>> net_default_options['dryrun'] = False
>>> command2()
>>> command3()
is a common use-case for me, both during development and in scripts
for occasional personal use. AFAICS this would break under your
suggestion.
Really, the only commonly-encountered problematic cases I can think of
are the anonymous empty objects, because they're typically used not
for their contents (d'oh), but rather as containers. pylint and
friends can easily detect [] and {} as default values for arguments.
> The barrier to this idea would be the code breakage involved; IMHO,
> code exploiting mutable defaults as static variables is in poor style
> anyway,
Sure, but it also breaks "globals" as above.
More information about the Python-ideas
mailing list