Mutable default values for function parameters

Terry Reedy tjreedy at
Fri Oct 5 00:46:42 CEST 2001

"Erik Max Francis" <max at> wrote in message
news:3BBCA084.B10B6061 at
> sebastien wrote:
> I avoid this problem by simply never using a mutable object as a
> argument, but instead always using a sentinel value that's
> in the function body:
> def sample(x, d = None):
>     if d is None:
>         d = []
>     d.append(x)
>     print d

This does not work when you *want* the altered value to carry over
from one invocation to the next, as was the case with both examples
given by the original poster.  The question was how to reinitialize
'd' (or whatever) when one wants to start over after several calls
using saved values.

Terry J. Reedy

More information about the Python-list mailing list