Default value for optional parameters unexpected behaviour?

Corey Richardson kb1pkl at aim.com
Sun Jun 26 15:23:57 EDT 2011


Excerpts from Thomas L. Shinnick's message of Sun Jun 26 14:53:21 -0400 2011:
> See reference manual section 7.6  "Function definitions" under the 
> discussion subtitle "Default parameter values are evaluated when the 
> function definition is executed. "
>          http://docs.python.org/reference/compound_stmts.html#function-definitions
> 
> Yes, this is discussed in many places and many times, but why isn't 
> it in the Python FAQ?  Amazing, yes?
> 

Well, to be fair, I don't think most people actually read the FAQ.
The FAQ does say:

"Default arguments can be used to determine values once, at compile
time instead of at run time. This can only be done for functions or
objects which will not be changed during program execution..."

And he did modify the list during program execution. However this
isn't exactly forthright if you aren't looking for it / know what
you're reading.  I don't think it should be spilled out in detail but
maybe a "there are some tricks involved with mutable default
arguments (for example a list).  Refer to the language reference
(LINK) for more details" would be useful.

But I'm not really certain that would make much of a difference.
I'll Cc this to docs at python.org.
-- 
Corey Richardson
  "Those who deny freedom to others, deserve it not for themselves"
     -- Abraham Lincoln
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20110626/8f496358/attachment.sig>


More information about the Python-list mailing list