Bugs item #1152726, was opened at 2005-02-26 22:10
Message generated for change (Comment added) made by rhettinger
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1152726&group_id=5470
Category: Python Interpreter Core
Group: Python 2.3
>Status: Closed
>Resolution: Invalid
Priority: 8
Submitted By: Simon Drabble (sdrabble)
Assigned to: Nobody/Anonymous (nobody)
Summary: Default class args get clobbered by prior instances.
Initial Comment:
OS: SuSE Linux 9.2 kernel 2.6.4
Python: 2.3.3
Define a class where the __init__() method takes a default arg of list
type. Instantiate this class, and append/ extend the value of its default
arg. Instantiate the class again, and the value from the first instance
will overwrite the second.
The bug is known to exist in 2.3.3, but may exist in subsequent
releases also.
The attached file illuminates the problem.
----------------------------------------------------------------------
>Comment By: Raymond Hettinger (rhettinger)
Date: 2005-02-27 08:18
Message:
Logged In: YES
user_id=80475
Sorry, this is not a bug. It is part of how Python works.
In general, do not use mutables for default values.
Instead, write something like:
def f(x, seq=None):
if seq is None:
seq = []
. . .
There is a FAQ on the subject:
http://www.python.org/doc/faq/general.html#why-are-default-values-shared-between-objects
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1152726&group_id=5470
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com