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: Invalid Behaviour When a Default Argument is a Mutable Object
Type: behavior Stage:
Components: Documentation Versions: Python 3.0, Python 3.1, Python 2.7, Python 2.6
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: LambertDW, Pasha2009, christian.heimes, georg.brandl, gregweb, loewis, rhr
Priority: normal Keywords:

Created on 2008-12-10 13:55 by rhr, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg77541 - (view) Author: Robert Hunger (rhr) Date: 2008-12-10 13:55
Reopening of issue 4181.

Evaluating default parameter values when the function definition is
executed is a design bug.

Even the documentation of this behaviour (see
http://docs.python.org/reference/compound_stmts.html#index-754) makes
this clear by stating: "This is generally not what was intended." It
then makes a suggestion for a workaround ("A way around this ..."). Only
bugs need workarounds.

An interface or behaviour should fulfil a users expectation for the
"normal" case, not for some "special" case. For a default parameter
value a user expects that whenever this function gets called a new
instance of a mutable object is created. This is much closer to the
current behaviour for non-mutable objects.

Usage cases for the current default behaviour are less common.
msg77542 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-12-10 14:27
No, it is NOT a bug. The behavior will NOT chance. I'm marking this bug
as a documentation issue.
msg77545 - (view) Author: Grégoire Weber (gregweb) Date: 2008-12-10 14:55
I'd like to second rhr. The current behaviour is is not according to
Pythons design principle of least surprise. After 5+ years of Python
experience as Zope and Plone developer I recently fell into that trap.

-----

Just for information (Discussion thread on a Pre-PEP started Feb 14, 2007:

  http://mail.python.org/pipermail/python-3000/2007-February/005704.html

The BDFL statement to this:

  http://mail.python.org/pipermail/python-3000/2007-February/005715.html
msg77549 - (view) Author: David W. Lambert (LambertDW) Date: 2008-12-10 15:14
Mutable function arguments and class variables are python's mechanisms
to provide data persistence at these scope levels.  Quite opposite of
design flaw, they are necessary!  Write a decorator to change the
behavior   where you'd like an empty mutable defaults if you're not
happy with other methods already described.
msg77573 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2008-12-10 22:32
I don't see the need to do anything about this. Python works correctly,
and the documentation correctly describes how it works, and how using
mutable objects as default values typically doesn't do what the
programmer normally expects.

In any case, a bug report is not the place to request such a change. At
a minimum, a PEP would have to be written (which then likely can't be
accepted before Python 4.0).
History
Date User Action Args
2022-04-11 14:56:42adminsetgithub: 48869
2008-12-10 22:32:29loewissetstatus: open -> closed
nosy: + loewis
resolution: wont fix
messages: + msg77573
2008-12-10 15:14:37LambertDWsetnosy: + LambertDW
messages: + msg77549
2008-12-10 14:55:59gregwebsetnosy: + gregweb
messages: + msg77545
2008-12-10 14:27:25christian.heimessetassignee: georg.brandl
messages: + msg77542
nosy: + georg.brandl
components: + Documentation, - Interpreter Core
versions: + Python 2.6, Python 3.1, Python 2.7, - Python 2.5, Python 2.4
2008-12-10 13:55:36rhrcreate