How to create multiple instances of a class?
Gilles Lenfant
glenfant at NOSPAM.bigfoot.com
Mon Mar 3 08:10:15 EST 2003
"Control Reset" <control_reset at hotmail.com> a écrit dans le message de news:
f5b03c44.0303030428.75fb012a at posting.google.com...
> Hello,
>
> How do I create multiple instances of a class?
>
> Lets say I have :
>
>
> class Node():
> def __init__(self,index):
> print "Index : %d", index
>
>
> #creat multiple instances
>
> nodes = []
> for i in range (10):
> nodes[i] = Node(i) <---
>
>
> The above gives an error, saying something like arrayoutofbounds
> array.
>
"nodes" is an empty list. How could your script get any item from this list
? That's why you got the exception !
Change the line that inits "nodes" againts this :
nodes = [None for dummy in range(10)]
Or you could prefer :
for i in range(10):
nodes.append(Node(i))
HTH
--Gilles
More information about the Python-list
mailing list