Somthing strange with list as default argument

From: Julien Sagnard (julien.sagnard.withoutthis_at_free.fr)
Date: 09/22/04


Date: Wed, 22 Sep 2004 10:19:45 +0200

Hi all,

When passing a list as default argument for a function, only one list is
created for all calls.
exemple:
>>> def f(a=[]):
... a.append("x")
... print a
...
>>> f()
['x']
>>> f()
['x', 'x']
>>> f()
['x', 'x', 'x']
>>> f()
['x', 'x', 'x', 'x']
>>>

I'm not found any reference of this. Is it a bug ?
I'm working on Windows 2000 with python 2.3.4



Relevant Pages

  • Re: newbie questions
    ... > Passing a pointer by value appears to me as passing a var by reference. ... Python doesn't support pass-by-reference in this standard use of the ... def clear: ...
    (comp.lang.python)
  • Re: Python advocacy in scientific computation
    ... int, you're passing a pointer to an int object, just ... pointer to a list object. ... It's just that Python ...
    (comp.lang.python)
  • Re: Type Hinting vs Type Checking and Preconditions
    ... Python community, both for tool enablement and for disambiguous ... Passing 14 to it will return 28, whereas passing "14" to it will return ... parameter to an int before it is multipled, ... little reason to have it crash. ...
    (comp.lang.python)
  • Re: Very strange: the code was good for the last 10 years and now it is stumbling!!!
    ... The code writing to Windows registry was good and working perfectly for the last 10 years and now suddenly it stops working ... the value you pass for the cbData parameter of RegSetValueEx must be 1 more than the length of the ... cbData must include the size of the terminating null character or characters.") So, you should be passing Len+1. ...
    (microsoft.public.vb.general.discussion)
  • Re: [Swig-user] How to receive a FILE* from Python under MinGW?
    ... I'm not sure that your snippet really solves my problem. ... see from, you're just passing a filename instead of a file pointer, ... FILE* from Python and all the way into into a existing shared-library ... There is fairly well-known problem on MinGW with passing FILE pointers ...
    (comp.lang.python)