mutable default arguments

Alex Martelli aleax at aleax.it
Thu Feb 27 07:22:58 EST 2003


Arnaud Delobelle wrote:

> 
> Hi everyone,
> 
> I am an 'amateur' programmer who's recently discovered python, and I
> must say it's great.  I had this problem though, it took me the
> whole morning to understand what it boils down to (I think).  So here is

Yes, your understanding is correct.

> So my question is: what's the rationale behind it?  I've spent so much

Simplicity.  Python does no "lazy evaluation": when it meets the
name=expression part, that's when it evaluates the expression.  It's
tricky, to be sure, since almost every beginner does get caught by
it (so did I when I was starting out) -- but by the same token just
about every Python book and tutorial out there warns about it;-).  And
once you start to look into it, and to specify exactly under what
conditions the default-argument expressions "should" be evaluated,
it's interesting to see how complicated it can become.  Not for
blah=[] by itself, of course, but clearly the rules should cover
every conceivable and legal possibility -- and the current rules
are as simple as can be, even though their result IS unexpected the
first time one gets bitten by it...


Alex





More information about the Python-list mailing list