python bug?

Anthony Petropoulos apetrop at gmail.com
Tue Jul 27 03:51:29 EDT 2004


Thanks to all who answered.

I was just confused because if I used assignment instead of .append
the thing worked. Too much java I guess *ducks*.

Peter Hansen's mail (among others') cleared it up for me.


On Mon, 26 Jul 2004 17:27:31 +0200, Thomas Guettler
<guettli at thomas-guettler.de> wrote:
> Am Mon, 26 Jul 2004 16:21:51 +0300 schrieb Anthony Petropoulos:
> 
> 
> 
> > Hi,
> >
> > When running this simple code:
> > -------
> > class Dada:
> >         a = []
> >         def __init__(self, arg):
> >                 self.a.append(arg)
> >
> > d1 = Dada("pro")
> > d2 = Dada("hoho")
> >
> > print d1.a, d2.a
> > ------------
> > I get the output ['pro', 'hoho'] ['pro', 'hoho'], instead of what I
> > expected: ['pro'] ['hoho'].
> >
> > Is this a feature? Is there something I'm missing?
> 
> your list "a" is at "class level". All instances
> share the same.
> 
> try this:
>     def __init__(self, arg):
>         self.a=[]
>         self.a.append(arg)
> 
> HTH,
>  Thomas
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list