Please explain this strange Python behaviour

Tim Chase python.list at
Thu Apr 30 13:46:55 CEST 2009

Train Bwister wrote:
> Please explain:
> IMHO this behaviour is anything but the usual straight forward and
> obvious way of Python.
> Can you please point out the benefits of this behaviour?

Note the section marked "Important warning" in bold.  That single 
dict-as-default-argument is evaluated once when the function is 
defined, and shared between function-objects (the things created 
by "def" statements).

The way to do it is:

   def my_method(self, your_param, d=None):
     if d is None:
       d = {}

There _are_ cases where it's a useful behavior, but they're rare, 
so I don't advocate getting rid of it.  But it is enough of a 
beginner gotcha that it really should be in the Python FAQ at


More information about the Python-list mailing list