Unexpected Python Behavior
Peter Otten
__peter__ at web.de
Fri Sep 24 10:30:46 EDT 2004
David Pokorny wrote:
>
> "Fredrik Lundh" <fredrik at pythonware.com> wrote in message >
>> it's a well-known "you'll only do this once" mistake. which is a good
> thing,
>
> "Because of this feature, it is good programming practice to not use
> mutable objects as default values." --
>
http://www.python.org/doc/faq/general.html#why-are-default-values-shared-between-objects
>
> Has it been discussed whether it would be a good idea to issue a warning
> in this case? It strikes me that a warning wouldn't bother veteran
> programmers, since it is really easy to avoid using a mutable default
> value (nearly trivial to modify code that does use mutable default
> values). I'd imagine it makes code more readable too.
You want a warning? There you are:
$ cat mutabledefault.py
def buggy(item, list=[]):
list.append(item)
return list
$ pychecker mutabledefault.py
Processing mutabledefault...
Warnings...
mutabledefault.py:3: Modifying parameter (list) with a default value may
have unexpected consequences
Peter
More information about the Python-list
mailing list