Somthing strange with list as default argument
Sam Holden
sholden at flexal.cs.usyd.edu.au
Wed Sep 22 04:40:08 EDT 2004
On Wed, 22 Sep 2004 10:19:45 +0200,
Julien Sagnard <julien.sagnard.withoutthis at free.fr> wrote:
> 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 ?
http://docs.python.org/tut/node6.html#SECTION006710000000000000000
(see the bold "important warning")
http://docs.python.org/ref/function.html
(see the paragraph with the bold text)
http://www.python.org/doc/faq/general.html
#why-are-default-values-shared-between-objects
(see the whole of 1.4.21 - link should be all one string with no new line...)
The tutorial, the reference docs, and the FAQ all mention this. There
isn't anywhere left to reference it.
--
Sam Holden
More information about the Python-list
mailing list