Re: bug or feature?
- From: Steve Holden <steve@xxxxxxxxxxxxx>
- Date: Wed, 05 Oct 2005 14:13:33 +0100
beza1e1:
Fredrik Lundh wrote:Coming back from a bug hunt, i am not sure what to think of this python behaviour. Here is a demo program:
class A: def __init__(self, lst=[]): self.lst = lst
a = A() b = A() b.lst.append("hallo") print a.lst # output: ["hallo"]
The point seems to be, that lst=[] creates a class attribute (correct name?), which is shared by all instances of A. So a.lst ist the same object as b.lst, despite the fact, that object a is different to object b.
I couldn't believe it wasn't, but you're right: it should be easier to find, and a change of wording may do that.Steve Holden wrote:
Interestingly I couldn't find this in the FAQ, though it *is* a frequently-asked question [note: my not finding it doesn't guarantee it's not there].
it's there:
http://www.python.org/doc/faq/general.html#why-are-default-values-shared-between-objects
(maybe "default values" should be changed to "default argument values")
regards Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC www.holdenweb.com PyCon TX 2006 www.python.org/pycon/
.
- References:
- bug or feature?
- From: beza1e1
- bug or feature?
- Prev by Date: 10060, Operation timed out Error
- Next by Date: Re: bug or feature?
- Previous by thread: Re: bug or feature?
- Next by thread: Python, Mysql, insert NULL
- Index(es):
Relevant Pages
|
Loading