bug or feature?

Steve Holden steve at holdenweb.com
Wed Oct 5 09:13:33 EDT 2005


beza1e1:

> 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.
> 
Fredrik Lundh wrote:
> 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")
> 
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.

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/




More information about the Python-list mailing list