Re: Bizarre behavior with mutable default arguments
- From: bukzor <workitharder@xxxxxxxxx>
- Date: Sat, 29 Dec 2007 20:21:08 -0800 (PST)
Just for completeness, the mutable default value problem also affects
classes:
class c:
def __init__(self, list = []):
self.list = list
self.list.append("LIST END")
def __repr__(self):
return "<Class a: %s>" % self.list
<Class a: ['LIST END']>import example2
print example2.c()
<Class a: ['LIST END']>print example2.c([])
<Class a: ['LIST END', 'LIST END']>print example2.c()
<Class a: ['LIST END']>print example2.c([])
Again, we get different results if we supply an argument that is
identical to the default value. There are many instances in the
standard library where class values are assigned directly from the
initializer, which has list or dict default values, so there is chance
for errors cropping up here.
The error scenario is this:
1. Use a mutable value as default value in a class constructor.
2. Assign class property from constructor arguments.
3. Instantiate class using default value.
4. Modify class property in place.
5. Instantiate (again) class using default value.
The second instance will behave strangely because data from the first
instance has leaked over. The standard library is not affected because
it avoids one of these five steps. Most classes simply don't have
mutable default values (1). Those that do generally treat them as read-
only (4). Some classes are not useful using the default values (3).
Some classes are not useful to be instantiated twice (5). The classes
that don't avoid the problem at one of these four steps have to avoid
it at (2) by using one of the three above patterns.
--Buck
.
- Follow-Ups:
- Re: Bizarre behavior with mutable default arguments
- From: Istvan Albert
- Re: Bizarre behavior with mutable default arguments
- From: John Machin
- Re: Bizarre behavior with mutable default arguments
- References:
- Bizarre behavior with mutable default arguments
- From: bukzor
- Re: Bizarre behavior with mutable default arguments
- From: "Martin v. Löwis"
- Re: Bizarre behavior with mutable default arguments
- From: Istvan Albert
- Re: Bizarre behavior with mutable default arguments
- From: bukzor
- Re: Bizarre behavior with mutable default arguments
- From: Steven D'Aprano
- Re: Bizarre behavior with mutable default arguments
- From: bukzor
- Bizarre behavior with mutable default arguments
- Prev by Date: Re: Bizarre behavior with mutable default arguments
- Next by Date: Re: Tab indentions on different platforms?
- Previous by thread: Re: Bizarre behavior with mutable default arguments
- Next by thread: Re: Bizarre behavior with mutable default arguments
- Index(es):
Relevant Pages
|