array of class

hg hg at nospam.org
Tue Jan 2 06:58:27 EST 2007


mm wrote:

> 
> How can I do a array of class?
> 
> s1=[]  ## this array should hold classes
> 
> ## class definition
> class Word:
>    word=""
> 
> 
> ## empty words... INIT
> for i in range(100):  ## 0..99
>    s1.append(Wort)
> 
> s1[0].word="There"
> s1[1].word="should"
> s1[2].word="be"
> s1[3].word="different"
> s1[4].word="classes"
> 
> ... but it's not.
> 
> 
> print s1
> ------------
> [<class __main__.Wort at 0x7ff1492c>,
> <class __main__.Wort at 0x7ff1492c>,
> <class __main__.Wort at 0x7ff1492c>,
> <class __main__.Wort at 0x7ff1492c>,
> <class __main__.Wort at 0x7ff1492c>,
> <class __main__.Wort at 0x7ff1492c>,
> ........
> -----------
> 
> Here, this "classes" are all at the same position in memory. So there
> are no different classes in the array.
> 
> So I access with s1[0], s1[1], s1[2], etc. always the same data.
> 
> Any idea?
> 
> --
> Michael


do you mean object ?

your append should be append(Word()) as you need to create instances.

hg






More information about the Python-list mailing list