always the same object (2)

Uwe Mayer merkosh at hadiko.de
Tue Jan 20 20:06:33 EST 2004


Uwe Mayer wrote:

Thanks for all the responses. As John Roth <newsgroups at jhrothjr.com> wrote
in <100rgj1etbcqr9d at news.supernews.com>:

> class DataWrapper():
>         data = { } 
>         def __init__(self, arg): #arg will contain a tuple
>                 data['var1'], data['var2'] = arg

the "data" variable is a class variable and shared by all DataWrapper,
therfore:

> result = [ ]
> while not <end-of-file f>:
>         data = struc.unpack("4s4s", f.read(8))
>         record = DataWrapper( data ) # pass tuple from unpack
>         result.append( record )

all "record"s in "result" share the same instance and thus all the data gets
overwritten. :)

Moving "data" into __init__() in DataWrapper does the trick.

Thanks again
Ciao
Uwe     



More information about the Python-list mailing list