On Thursday 19 April 2007, [EMAIL PROTECTED] 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!
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] >>> a.append(a) >>> for i in a: print i 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 ;). -- http://mail.python.org/mailman/listinfo/python-list