List of Objects

Mel Wilson mwilson at the-wire.com
Thu Apr 19 23:27:00 EDT 2007


datamonkey.ryan at gmail.com wrote:
> Howdy, a (possibly) quick question for anyone willing to listen.
> I have a question regarding lists and Classes; I have a class called
> "gazelle" with several attributes (color, position, etc.) and I need
> to create a herd of them. I want to simulate motion of individual
> gazelles, but I don't want to have to go through and manually update
> the position for every gazelle (there could be upwards of 50). I was
> planning to create an array of these gazelle classes, and I was going
> to iterate through it to adjust the position of each gazelle. That's
> how I'd do it in C, anyway. However, Python doesn't support pointers
> and I'm not quite sure how to go about this. Any help you can provide
> would be greatly appreciated.
> Thanks a lot!

You don't want a herd of the Platonic type of gazelle, you want a herd 
of individual instances of the class of gazelle.  No problem.
Something like

class Gazelle(object):
     def __init__ (self, color, position)
         self.color = color
         self.position = position
# ...
herdsize = 30
herd = []
for i in xrange (herdsize)
     color, position = function_to_supply_a_gazelle's_attributes()
     herd.append (Gazelle (color, position)
# ...
while true:
     for gazelle in herd:
         do_something_to (gazelle)

> 
> -Ryan
> 




More information about the Python-list mailing list