Class Chaos

Yermat loic at fejoz.net
Mon Jun 28 12:06:35 CEST 2004


Maximilian Michel wrote:
> Hallo everyone, 
> 
> i hope someone can help me with this kind a interesting problem i have
> in python 2.3:
> 
> my code looks like this:
> 
> class Read:
>      list = []
>      __init__(self):
>               self.list.append = ***data from file***
>      print(self):
>               print self.list
> 
> instantiating it one time works ok, assuming data from file = AAA:
>        ...
>        a = Read()
>        AAA
> but when i continue, instantiating more object of the Read class this
> happens:
>        b = Read()
>        AAA
>        AAA
>        c = Read()
>        AAA
>        AAA
>        AAA
> it's like the new instance always continues or reimports the data from
> the former instance.
> Or is it just a dumb mistake i keep making?

Of course !
list is a class attribute so it is share by all instances !
You should initialize list in the __init__ method :

class Read:
     def __init__(self):
         self.list = []
         self.list.append...


-- 
Yermat




More information about the Python-list mailing list