Bizarre behavior with mutable default arguments

Neil Cerutti mr.cerutti at gmail.com
Tue Jan 1 12:11:11 EST 2008


On Jan 1, 2008 12:26 AM, NickC <ncoghlan at gmail.com> wrote:

> On Jan 1, 3:22 am, Arnaud Delobelle <arno... at googlemail.com> wrote:
> > On Dec 31, 10:58 am, Odalrick <odalr... at hotmail.com> wrote:
> >
> > > 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.
>
> As Odalrick notes, there is no way to give different calls to a
> function their own copies of mutable default arguments without re-
> evaluating the defaults every time the function is called.


There's a policy solution: stipulate that default arguments must be
immutable (or hashable, as Python prefers to call it).

The
> horrendous performance implications mean that that simply isn't going
> to happen.


Are the performance implications truly horrendous, though? Considering that
the "poorly performing" solution seems to be the functionality that the
uninformed expect, perhaps they *want* it to perform that way?

-- 
Neil Cerutti
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080101/94c08cc9/attachment-0001.html>


More information about the Python-list mailing list