[Tutor] Instantiating large numbers of objects? [lists and
dictionaries]
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Sun, 3 Feb 2002 14:03:20 -0800 (PST)
On Sun, 3 Feb 2002, Britt Green wrote:
> If you had three accounts, you'd create three instances in your code:
>
> bob = new Account()
> sue = new Accout()
> jim = new Account()
>
> and then you'd call the class methods like so:
>
> bob.makeWithdrawl(500)
> sue.checkBalance()
> jim.makeDeposit(200)
>
> So far this is the only way I know of to instantiate classes.
> Obviously when a bank uses this software, they wouldn't open up the
> code to add new accounts. How would they create new instances of the
> Account class? I seem to recall being told once that in C or Java one
> would create an array or Vector of objects. I was thinking I would do
> the same thing with my Rooms, except Python doesn't have arrays or
> Vectors.
Although Python doesn't have arrays or Vectors, it does have a similar
container class: Python has the 'list' type. Here's an example:
###
>>> class Person:
... def __init__(self, name):
... self.name = name
... def sing(self):
... print "tra la la, my name is", self.name
...
>>> names = ['bart', 'lisa', 'marge', 'homer']
>>> people = []
>>> for n in names:
... people.append(Person(n))
...
>>> people
[<__main__.Person instance at 80a2b28>, <__main__.Person instance at
80d3498>, <
__main__.Person instance at 80d34d0>, <__main__.Person instance at
80d3540>]
###
In this example, we assign 'people' initially to the empty list, '[]'.
If we're given some list of names, we can easily create instances for each
name, and add them to our list. By using a list, we avoid hardcoding the
number of people our program can deal with.
The append() method on Python lists is equivalent to the "push_back()"
method in C++ vectors. Once we have this list of people, we can pick any
one of them by index:
###
>>> people[0].sing()
tra la la, my name is bart
###
But people often don't like to be labeled with numbers. It might be nicer
to be able to say something like:
people['bart'].sing()
That is, it might be nice to use the person's name to pull them from our
list container. In that case, we can use a more appropriate data
structure, the dictionary:
###
>>> people_dict = {}
>>> for n in names:
... people_dict[n] = Person(n)
...
>>> people_dict['lisa'].sing()
tra la la, my name is lisa
###
A Python dictionary is equivalent to Java's concept of a 'Map'. What's
nice about Python's dictionary is that it uses very similar syntax to that
of arrays --- this emphasizes the idea that Lists and Dictionaries
implement the same idea of a container.
Also, both of these data types are considered "primitive" in Python, which
makes them very convenient to work with. Take a look at:
http://www.python.org/doc/tut/node5.html#SECTION005140000000000000000
for an example of List usage. I think you'll find that Python lists are
pretty darn casual compared to a C++ array. *grin*
For information on Python dictionaries, see:
http://www.python.org/doc/tut/node7.html#SECTION007400000000000000000
There's a lot of good information on these data types in almost every
Python tutorial:
http://python.org/doc/Intros.html
and please feel free to ask questions about them; lists and dictionaries
are fun to talk about. *grin*
> I apologize if I'm not explaning myself clearly. This is still pretty
> new to me and I'm not 100% sure how to phrase my question.
Don't worry: your question makes perfect sense. I hope this answer
somewhat reciprocates that. Good luck to you!