This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Misleading behavior for [] and {} default arguments
Type: Stage:
Components: Interpreter Core Versions: Python 2.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, matthijsd
Priority: normal Keywords:

Created on 2007-02-16 15:32 by matthijsd, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg31280 - (view) Author: Matthijs (matthijsd) Date: 2007-02-16 15:32
Hello

On python 2.3.4 and 2.4.3, 

>>> def a(x=[]):
...   return x
... 
>>> y=a()
>>> y.append(1)
>>> a()
[1]

Hence, the default argument is not recomputed. The same behavior occurs with x={} instead of x=[].

This looks like a bug because it is not coherent with 

>>> def f():
...   return []
... 
>>> y=f()
>>> y.append(5)
>>> f()
[]

So, is it a feature?

Thanks

msg31281 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-02-16 15:40
Yes, it is. (Please post questions about Python semantics to the comp.lang.python newsgroup, they'll explain them in detail...)
History
Date User Action Args
2022-04-11 14:56:22adminsetgithub: 44588
2007-02-16 15:32:14matthijsdcreate