array of class

Carl Banks pavlovevidence at gmail.com
Tue Jan 2 17:22:41 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.

I presume you want an list (not array) of objects (not classes).  In
that case, you're missing parentheses after Word.  You have to call the
class object, same as you'd call a function, so you have to follow it
with parentheses:

s1.append(Word())

You could, in fact, have an array of classes, and there are actually
reasons you might want to do that, but that's pretty advanced.


Carl Banks




More information about the Python-list mailing list