Class Chaos
Helmut Jarausch
jarausch at igpm.rwth-aachen.de
Mon Jun 28 06:06:50 EDT 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.
The assignment list=[] is made only once when the class definition
gets compiled. So your __init__ method uses one and the same list object
all the time.
When you create a new object of class 'Read' only the __init__ method gets
executed.
If you shift the 'list=[]' statement within the __init__ method it will
work as you probably like it to.
--
Helmut Jarausch
Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
More information about the Python-list
mailing list