functions, list, default parameters

Dan Stromberg drsalists at gmail.com
Fri Oct 29 14:14:44 EDT 2010


On Thu, Oct 21, 2010 at 7:53 PM, John Nagle <nagle at animats.com> wrote:

> On 10/21/2010 2:51 PM, Chris Rebert wrote:
>
>> On Thu, Oct 21, 2010 at 2:36 PM, Sean Choi<gnemnk at gmail.com>  wrote:
>>
>>> I found two similar questions in the mailing list, but I didn't
>>> understand
>>>
>>> the explanations.
>>> I ran this code on Ubuntu 10.04 with Python 2.6.5.
>>> Why do the functions g and gggg behave differently? If calls gggg(3) and
>>> g(3) both exit their functions in the same state, why do they not enter
>>> in
>>> the same state when I call gggg(4) and g(4)?
>>>
>>> # ----------------------------------------------------------------------
>>> my
>>> code:
>>> def gggg(a, L=[]):
>>>
>>
>> This is a common newbie stumbling-block: Don't use lists (or anything
>> mutable) as default argument values
>>
>
>    That really should be an error.


Pylint warns about it.  I think that's enough.

I think it's enough, in part because unfortunately default values have
become overloaded as the way of creating variables with a global lifetime,
and sometimes that's useful to have.  I think it would probably have been
better to make default arguments be evaluated once per function/method
invocation, and add a static modifier to variables, but oh well.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101029/fa74b206/attachment-0001.html>


More information about the Python-list mailing list