Bizarre behavior with mutable default arguments

Odalrick odalrick at hotmail.com
Mon Dec 31 15:08:22 EST 2007


On 31 Dec, 18:22, Arnaud Delobelle <arno... at googlemail.com> wrote:
> 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.
>

Adding overhead to *all* function calls, even the ones without mutable
defaults. That doesn't sound like an attractive tradeoff.

> 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