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 10 1830
datamonkey.r... @gmail.com wrote:
>
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(G azelle())
for g in g_list:
g.x = 10
print g_list[2].x
On Apr 20, 3:58 am, datamonkey.r... @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!
-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.
On Thursday 19 April 2007, da************* @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!
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 ;).
On Thu, 19 Apr 2007 19:58:35 -0700, datamonkey.ryan 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.
First method: create 1000 different gazelles:
list_of_beastie s = []
for i in xrange(1000): # create 1000 beasties
args = (i, "foo", "bar") # or whatever
list_of_beastie s.append(Gazell e(args))
Second method: create 1000 different gazelles by a slightly different
method:
list_of_beastie s = [Gazelle((i, "foo", "bar")) for i in xrange(1000)]
Third method: create 1000 copies of a single gazelle:
list_of_beastie s = [Gazelle(args)] * 1000
# probably not useful...
Forth method: create identical gazelles, then modify them:
list_of_beastie s = [Gazelle(default s) for i in xrange(1000)]
for i, beastie in enumerate(xrang e(1000)):
list_of_beastie s[i] = modify(beastie)
--
Steven D'Aprano
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!
On Apr 19, 11:22 pm, Steven D'Aprano
<s...@REMOVEME. cybersource.com .auwrote:
On Thu, 19 Apr 2007 19:58:35 -0700, datamonkey.ryan 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.
First method: create 1000 different gazelles:
list_of_beastie s = []
for i in xrange(1000): # create 1000 beasties
args = (i, "foo", "bar") # or whatever
list_of_beastie s.append(Gazell e(args))
Second method: create 1000 different gazelles by a slightly different
method:
list_of_beastie s = [Gazelle((i, "foo", "bar")) for i in xrange(1000)]
Third method: create 1000 copies of a single gazelle:
list_of_beastie s = [Gazelle(args)] * 1000
# probably not useful...
Forth method: create identical gazelles, then modify them:
list_of_beastie s = [Gazelle(default s) for i in xrange(1000)]
for i, beastie in enumerate(xrang e(1000)):
list_of_beastie s[i] = modify(beastie)
--
Steven D'Aprano
da************* @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_sup ply_a_gazelle's _attributes()
herd.append (Gazelle (color, position)
# ...
while true:
for gazelle in herd:
do_something_to (gazelle)
>
-Ryan
<da************ *@gmail.comwrot e:
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, miscommunicatio n's likely:-(.
Alex
Steven D'Aprano wrote:
[...]
>
Forth method: create identical gazelles, then modify them:
list_of_beastie s = [Gazelle(default s) for i in xrange(1000)]
for i, beastie in enumerate(xrang e(1000)):
list_of_beastie s[i] = modify(beastie)
Nope, 'sorry, that's Python a's well. Forth u'se's rever'se Poli'sh
notation.
regard's
"Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com
On Apr 19, 9:18 pm, Paddy <paddy3...@goog lemail.comwrote :
>
# 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: oom |
last post by:
I am a bit of a newbie when it comes to python, when working with
lists today I noticed some very odd behaviour, any suggestions
welcome:
Python 2.2.3 (#1, Nov 6 2003, 14:12:38)
on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> firstlist=
>>> secondlist=firstlist
>>> print (firstlist,secondlist)
|
by: dasod |
last post by:
I would like to know if my method to remove list objects is correct in
this small test program. It seems to me that there might be a simplier
way, but I'm afraid I don't know enough about list iterators and how
they are behaving in situations like this.
#include <iostream>
#include <list>
class Test;
typedef std::list< Test* > Tlist;
|
by: JustSomeGuy |
last post by:
I have an stl list that grows to be too huge to maintain effectivly in
memory.
There are elements within the list that could be stored on disk until
accessed.
However I don't want to expose this to the application class.
How can I extent the stl list to write some elements to disk when they
are put in the list
and read them from disk when they are read from the list.
|
by: Glen Able |
last post by:
Hi,
I have a collection of lists, something like this:
std::list<Obj*> lists;
Now I add an Obj to one of the lists:
Obj* gary;
lists.push_front(gary);
|
by: Christoph Zwerschke |
last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist
only since Python 2.4.
Anyway, I was thinking about whether it would be possible and desirable
to change the old behavior in future Python versions and let dict.keys()
and dict.values() both return sets instead of lists.
If d is a dict, code like:
for x in d.keys():
| |
by: Little |
last post by:
I have this program and I need to work on the test portion, which tests
if a Val is in the list. It returns false no matter what could you look
at the part and see what might need to be done to fix it. It reads in
the file and sorts out the files into the four different lists.
F.txt
int main
2
" "
help
|
by: Ville Vainio |
last post by:
I tried to clear a list today (which I do rather rarely, considering
that just doing l = works most of the time) and was shocked, SHOCKED
to notice that there is no clear() method. Dicts have it, sets have it,
why do lists have to be second class citizens?
|
by: Jimp |
last post by:
Why can't I cast List<MyObject> to ICollection<IMyObject>.
MyObject implements IMyObject, and of course, List implements
ICollection.
Thanks
|
by: Macca |
last post by:
Hi,
My app needs to potentially store a large number of custom objects and be
able to iterate through them quickly. I was wondering which data structure
would be the most efficient to do this,a hashtable or a generic list.
Is using enumerators to iterate through the data structure a good idea?
I'd appreciate any suggesstions or advice,
|
by: Amit Bhatia |
last post by:
Hi,
I am not sure if this belongs to this group. Anyway, my question is as
follows: I have a list (STL list) whose elements are pairs of integers (STL
pairs, say objects of class T). When I create a new object of class T, I
would like to check if this object already exists in the list: meaning one
having same integers. This can be done in linear time in a list, and
probably faster if I use STL Set instead of list. I am wondering however if...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |