Mutable default values for function parameters

Terry Reedy tjreedy at home.com
Thu Oct 4 18:46:42 EDT 2001


"Erik Max Francis" <max at alcyone.com> wrote in message
news:3BBCA084.B10B6061 at alcyone.com...
> sebastien wrote:
>
> I avoid this problem by simply never using a mutable object as a
default
> argument, but instead always using a sentinel value that's
substituted
> 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