How to create multiple instances of a class?

Alex Martelli aleax at aleax.it
Mon Mar 3 08:10:35 EST 2003


<posted & mailed>

Control Reset wrote:

> Hello,
> 
> How do I create multiple instances of a class?

By calling the class multiple times.  Each time you call the class
(with suitable parameters) you create an instance.


> Lets say I have :
> 
> class Node():
>    def __init__(self,index):
>       print "Index : %d", index

So far so good -- as indeed you do below, you need to call Node
with exactly one parameter.  I'm not sure why you WANT to print 
something like

   Index : %d 23

so maybe you mean to use a % instead of a comma in the print
statement?  But that doesn't affect your ability to create
instances of this class, it just gives strange output.  And
similarly for the fact that you don't "save" the value of
index anywhere (with a statement such as self.index = index
in your init method) -- it's strange but not necessarily an
error and doesn't affect your ability to create instances.


> #creat multiple instances
> 
> nodes = []
> for i in range (10):
>   nodes[i] = Node(i)      <---
> 
> 
> The above gives an error, saying something like arrayoutofbounds
> array.

Well of course - this has obviously nothing to do with the
ability to create multiple instances of a class, which you
mention in both the Subject: header and in the question at
the start of your post.  To convince yourself of that, just
change the assignment in the loop to, e.g.:
    nodes[i] = i
and you will get exactlythe same problem.  Arrays are not
in the pictures, but lists are -- specifically, a list which
you initialize as empty (so that no indices into it are
valid) and then try to index anyway.

To add an item to the end of a list, the simplest way is
to call the append method of the list.  So, to create the
list of ten instances of Node that you seem to want,
change the loop body to:
    nodes.append(Node(i))
and you're all set.

A more direct way to do the same task is:

nodes = [Node(i) for i in range(10)]

creating the list directly rather than having it start
out empty and appending one item at a time.  This kind
of construct is known as a "list comprehension".


> Could you please advise.
> 
> Which basically boils down to the question on how to use list.

Yes, it does seem the question you posed in the subject has
just about nothing to do with your actual problem.


> 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?

If what you want is a list of lists, create a list of lists:

class Node:
    def __init__(self, i, j):
        self.ij = i, j

nodes = [ [Node(i,j) for i in range(10)] for j in range(5) ]

or the like.  Now, you can access nodes[i][j] for suitable values
of i and j (not node, but nodes; if you do want to name your list
of lists 'node' without a trailing s, as it would now appear from
this latest paragraph of yours that I've quoted, just change the
name on the left of the = sign appropriately).


Alex





More information about the Python-list mailing list