python bug?

Russell Blau russblau at hotmail.com
Mon Jul 26 09:57:19 EDT 2004


"Anthony Petropoulos" <apetrop at gmail.com> wrote in message
news:mailman.815.1090848123.5135.python-list at python.org...
> 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?

You established "a" as an attribute at the class level, not at the instance
level.  Then you had each instance append something to the class attribute.
The output is correct; it is your expectation that needs to be modified.
:-)

To see the difference, try changing the body of your __init__ method to
read:
    self.a = [arg]

-- 
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.





More information about the Python-list mailing list