How to create multiple instances of a class?

user at domain.invalid user at domain.invalid
Mon Mar 3 13:58:20 CET 2003


Control Reset wrote:
> 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)      <---
>
shouldn't this be :

   nodes.insert(i,Node(i) ) *

or
   nodes.append( Node(i)) *
> 
> The above gives an error, saying something like arrayoutofbounds
> array.
> 
That's probably because you have a list of an undefined size, without
entries, hence you cannot do "nodes[i]" as  that implies that list/
array has an i-th element.
> Could you please advise.
> 
> Which basically boils down to the question on how to use list.
> 
See above *
> I think   nodes = Node(i)   will work but I dont want a list in
> sequence. Basically what I want is a 2D array which I can access via
> an index, eg node[i][j]. How do I do this?
> 
> Please advise?
> 
> Thank you.
> Ctrl-Reset





More information about the Python-list mailing list