python bug?

Jeff Sandys sandysj at juno.com
Mon Jul 26 11:50:47 EDT 2004


Is this what you meant to do?

>>> class Dada:
...	a = []
...	def __init__(self,arg):
...		Dada.a.append(arg)
...		self.a = Dada.a
... 
>>> d1 = Dada("pro")
>>> d2 = Dada("hobo")
>>> d1.a
['pro', 'hobo']
>>> d2.a
['pro', 'hobo']
>>> Dada.a
['pro', 'hobo']
>>> 

Thanks,
Jeff Sandys

Anthony Petropoulos wrote:
> 
> 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?
> 
> Anthony Petropoulos



More information about the Python-list mailing list