List of Objects

D

datamonkey.ryan

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!

-Ryan
 
7

7stud

However, Python doesn't support pointers

As I understand it, every name in Python is a pointer.

class Gazelle(object):
def __init__(self):
self.x = 0

g_list =[]
for x in range(10):
g_list.append(Gazelle())

for g in g_list:
g.x = 10

print g_list[2].x
 
P

Paddy

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!

-Ryan

Something like:

import random
class Gazelle(object):
def __init__(self):
self.pos = 0, 0

# create a list of instances
gazelles= [ Gazelle() for x in range(5)]

# update gazelle positions
deltaxmin, deltaxmax = -100, +100
deltaymin, deltaymax = -100, +100
for g in gazelles:
g.pos = (g.pos[0] + random.randint(deltaxmin, deltaxmax),
g.pos[1] + random.randint(deltaymin, deltaymax) )

The above is untested by the way.

- Paddy.
 
D

DillonCo

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!

Actually, Python _only_ supports pointers: they're the names of objects. So
for example, if I write x = Gazelle(...), then I create the name "x" that
points to an instance of Gazelle. The storage for the instance is
managed 'magically' by Python. If I were then to say "y = x", I'd also have
a name "y" that points to the same instance.

It's also worth noting that everything, including ints, strings and lists are
objects as well. Because of this, a pointer to an object is the only storage
class in python. Therefore, a name can point to any object of any type.

As a result, an "array" in Python, which is commonly a list, is simply a list
of pointers. They can point to strings, ints, other lists or anything. And
because they store pointers, they can actually include themself!

To demonstrate:
a=[1,'a',[1,2,3]]
for i in a: print i
1
a
[1, 2, 3]1
a
[1, 2, 3]
[1, 'a', [1, 2, 3], [...]]


Python's clever enough to not print out the circular reference.

Finally, it's worth pointing out that in a language like this, where there are
no arbitrary pointers (as there are in C), the pointer-to-object is called a
reference. I just used "pointer" because you did ;).
 
S

Steven D'Aprano

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.

First method: create 1000 different gazelles:

list_of_beasties = []
for i in xrange(1000): # create 1000 beasties
args = (i, "foo", "bar") # or whatever
list_of_beasties.append(Gazelle(args))


Second method: create 1000 different gazelles by a slightly different
method:

list_of_beasties = [Gazelle((i, "foo", "bar")) for i in xrange(1000)]

Third method: create 1000 copies of a single gazelle:

list_of_beasties = [Gazelle(args)] * 1000
# probably not useful...

Forth method: create identical gazelles, then modify them:

list_of_beasties = [Gazelle(defaults) for i in xrange(1000)]
for i, beastie in enumerate(xrange(1000)):
list_of_beasties = modify(beastie)
 
D

datamonkey.ryan

These methods work. I didn't think I could create a list of objects
like that, however, I stand corrected.
Thanks for your quick (and helpful) responses!


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.

First method: create 1000 different gazelles:

list_of_beasties = []
for i in xrange(1000): # create 1000 beasties
args = (i, "foo", "bar") # or whatever
list_of_beasties.append(Gazelle(args))

Second method: create 1000 different gazelles by a slightly different
method:

list_of_beasties = [Gazelle((i, "foo", "bar")) for i in xrange(1000)]

Third method: create 1000 copies of a single gazelle:

list_of_beasties = [Gazelle(args)] * 1000
# probably not useful...

Forth method: create identical gazelles, then modify them:

list_of_beasties = [Gazelle(defaults) for i in xrange(1000)]
for i, beastie in enumerate(xrange(1000)):
list_of_beasties = modify(beastie)
 
M

Mel Wilson

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)
 
A

Alex Martelli

Howdy, a (possibly) quick question for anyone willing to listen.
I have a question regarding lists and Classes; I have a class called

It looks you don't really want what you're saying: you appear to want a
list of INSTANCES of one class, *NOT* a list of CLASSES. E.g.:

class Gazelle(object): pass

class Zip(Gazelle): pass

class Zop(Gazelle): pass

class Zap(Gazelle): pass

thelist = [Zip, Zop, Zap]


Now THIS would be a list of classes, but contextual clues in your text
appear to suggest that you do NOT want this, and may be deeply mistaken
about what "a list of classes" means. I'd rather get confirmation of
that point before I address your question; if you use totally,
irretrievably wrong terminology, miscommunication's likely:-(.


Alex
 
S

Steve Holden

Steven D'Aprano wrote:
[...]
Forth method: create identical gazelles, then modify them:

list_of_beasties = [Gazelle(defaults) for i in xrange(1000)]
for i, beastie in enumerate(xrange(1000)):
list_of_beasties = modify(beastie)

Nope, 'sorry, that's Python a's well. Forth u'se's rever'se Poli'sh
notation.

regard's
"Steve
 
7

7stud

# create a list of instances
gazelles= [ Gazelle() for x in range(5)]

Nice. I knew there had to be a way to use a list comprehension, but I
couldn't figure it out.
 
H

Hendrik van Rooyen

Steven D'Aprano wrote:
[...]
Forth method: create identical gazelles, then modify them:

list_of_beasties = [Gazelle(defaults) for i in xrange(1000)]
for i, beastie in enumerate(xrange(1000)):
list_of_beasties = modify(beastie)

Nope, 'sorry, that's Python a's well. Forth u'se's rever'se Poli'sh
notation.


No it wa's a biblical incantation - "go forth and multiply..."

- Hendrik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top