keyword arguments and **
Boudewijn Rempt
boud at valdyas.org
Sat Nov 16 06:50:52 EST 2002
<posted & mailed>
Alex Martelli wrote:
>
> Usual problem: when an argument (here, fields) has a default
> value that is mutable, that value is determined ONCE, when the
> def statement executes -- then that single value stays around
> (the function object references it), and if you mutate it you
> get such a "memory effect".
>
> Usual fix:
>
>>>> def f(fields=None, **args):
> ... if fields is None: fields = {}
> ... print fields
> ... fields.update(args)
> ... print "XXX", fields
>
Thanks. I feel even sillier now :-).
--
Boudewijn Rempt | http://www.valdyas.org
More information about the Python-list
mailing list