Bizarre behavior with mutable default arguments

Arnaud Delobelle arnodel at googlemail.com
Mon Dec 31 12:22:14 EST 2007


On Dec 31, 10:58 am, Odalrick <odalr... at hotmail.com> wrote:
> On 30 Dec, 17:26, George Sakkis <george.sak... at gmail.com> wrote:
>
> > On Dec 29, 9:14 pm, bukzor <workithar... at gmail.com> wrote:
>
> > > Here's the answer to the question:http://www.python.org/doc/faq/general/#why-are-default-values-shared-...
>
> > > It looks like Guido disagrees with me, so the discussion is closed.
>
> > Note that the FAQ mainly explains *what* happens, not *why* was this
> > decision taken. Although it shows an example where "this feature can
> > be useful", it's neither the only way to do it nor is memoization as
> > common as wanting fresh default arguments on every call.
>
> I'm surprised noone has said anything about the why of default
> mutables. I think it is becasue it isn't easy to do it an other way.

[...]

There is an easy enough way: evaluate default values when the function
is called rather than when it is defined.  This behaviour comes with
its own caveats as well I imagine, and it's not 'as easy' to implement
as the current one.

What's good about the current behaviour is that it is easy to reason
with (once you know what happens), even though you almost have to get
bitten once.  But using this to have static variable is extremely ugly
IMHO.

--
Arnaud




More information about the Python-list mailing list