[Python-3000] pre-PEP: Default Argument Expressions
Greg Ewing
greg.ewing at canterbury.ac.nz
Thu Feb 15 01:54:29 CET 2007
BJörn Lindqvist wrote:
> I have many, MANY times written bugged code like this:
>
> def something(x = None):
> if not x:
> x = 42 # <- Oh noe!
You can get exactly the same bug in many other contexts
besides default arguments. It's something you need to
be on the alert for generally, and if you are, you are
no more likely to encounter it here than anywhere else.
> def something(x = []):
> x += ["foobar"] # <- Even worse!
I'm skeptical that people really write functions that
do things like that. It smells wrong: Is the function
intended to mutate an argument that's passed in? If
not, then it shouldn't be touching the argument, in
which case it doesn't matter if the default value is
evaluated only once. If so, and no argument is passed,
it would be more efficient to just skip the code that
does the mutation, rather than create a new list,
mutate it, and then throw it away.
> Me, newbies, lazy programmers or programmers with not enough attention
> to details.
Anyone who can't pay attention to details is going to
have much bigger problems with programming than just
dealing with default arguments.
--
Greg
More information about the Python-3000
mailing list