python bug?
James Henderson
james at logicalprogression.net
Mon Jul 26 09:30:00 EDT 2004
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
Hi Anthony,
You've made "a" an attribute of the class, which means that it is shared
by all instances. To make it an instance attribute as you intended you
should have written:
class Dada:
def __init__(self, arg):
self.a = [arg]
HTH,
James
More information about the Python-list
mailing list