473,320 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Dynamically naming objects.

I've been developing a small script to fiddle with classes, and came
accross the following problem. Assuming I get some user input asking
for a number, how would I spawn 'n' objects from a class?

i.e. I have a class class 'user' and I don't know how many of them I
want to spawn.

Any ideas?
Jun 27 '08 #1
8 1496
Kalibr wrote:
I've been developing a small script to fiddle with classes, and came
accross the following problem. Assuming I get some user input asking
for a number, how would I spawn 'n' objects from a class?

i.e. I have a class class 'user' and I don't know how many of them I
want to spawn.

Any ideas?
Sure. This will give you a list of n instances of user:

[user() for i in range(n)]

Of course, you could also use a good old for loop:

for i in range(n):
u = user()
...do something with u...

Hope this helps!

--
Hans Nowak (zephyrfalcon at gmail dot com)
http://4.flowsnake.org/
Jun 27 '08 #2
On Jun 7, 1:20 pm, Hans Nowak <zephyrfalcon!NO_SP...@gmail.comwrote:
Kalibr wrote:
I've been developing a small script to fiddle with classes, and came
accross the following problem. Assuming I get some user input asking
for a number, how would I spawn 'n' objects from a class?
i.e. I have a class class 'user' and I don't know how many of them I
want to spawn.
Any ideas?

Sure. This will give you a list of n instances of user:

[user() for i in range(n)]

Of course, you could also use a good old for loop:

for i in range(n):
u = user()
...do something with u...

Hope this helps!

--
Hans Nowak (zephyrfalcon at gmail dot com)http://4.flowsnake.org/
whoops, replied to author....

What I wanted to ask before was won't 'u' be overwritten with a new
object each time the loop ticks over?

what I want to do is have, say 5 users in a game, so I'd have to spawn
5 objects. I can't do that because I have'nt hardcoded any object
names for them.

or does it somehow work? how would I address them if they all have the
name 'u'?
Jun 27 '08 #3
Kalibr <sp****************@gmail.comwrites:
what I want to do is have, say 5 users in a game, so I'd have to
spawn 5 objects. I can't do that because I have'nt hardcoded any
object names for them.
Python's built-in mapping type 'dict' is a good fit for this.

Given:

* a 'User' class that is initialised with the user's name

* some way of getting a sequence of names (that you haven't told us
yet), that I'll bind here to the name 'sequence_of_names'

You can then write::

game_users = {}
for name in sequence_of_names:
game_users[name] = User(name)

This will result in 'game_users' bound to a dict with names mapping to
separate instances of the 'User' type. These instances can each be
addressed by name from the 'game_users' mapping as
'game_users["Fred"]', etc.

--
\ "Pinky, are you pondering what I'm pondering?" "Well, I think |
`\ so, Brain, but do I really need two tongues?" -- _Pinky and |
_o__) The Brain_ |
Ben Finney
Jun 27 '08 #4
On Jun 7, 1:20 pm, Hans Nowak
> [user() for i in range(n)]

Kalibr wrote:
or does it somehow work? how would I address them if they all have the
name 'u'?

users = list(User() for i in range(n))
for user in users:
user.do_something()

hth,
Alan Isaac

Jun 27 '08 #5
Hello,

You can use this as indicated by Hans:
> u = [user() for i in xrange(5)]
where "user" is a class or a function returning an object.
u then is a list of "user" objects.
or does it somehow work? how would I address them if they all have the
name 'u'?
You'd access members of the list as u[0], u[1], ... etc.
I think you'd benefit from reading an introductory programming book.

Best regards,
Stefaan.
Jun 27 '08 #6
Thanks for all this info.

I'll try all your scripts out.

from what you guys have said, I did the following:

I set up a 'computer class' (I'lm leaving out the mutators)

class computer:

def __init__(self, IP, owner, ph_connections, connections):

assert isIP(IP) == True
self.IP = IP
self.owner = owner

for i in ph_connections:
assert isIP(i) == True
self.ph_connections = ph_connections

for i in connections:
assert isIP(i) == True
self.connections = connections

isIP(IP) is a function that checks if it looks like an IP based on
some rules.

Anyway....

I set up a list of users, each with their computer object named after
them, so:
users = ['Kal', 'Noob', 'Fred']

I ran through them with some code based vaguely on what you guys have
said:

for i in users:
i = computer('<insert some sort of IP here>', i, [], [])

I set up the ph_connections and connections lists as empty for ease of
use.
Does this code seem right? I can't get it to work.
Jun 27 '08 #7
Kalibr wrote:
On Jun 7, 1:20 pm, Hans Nowak <zephyrfalcon!NO_SP...@gmail.comwrote:
>Kalibr wrote:
>>I've been developing a small script to fiddle with classes, and came
accross the following problem. Assuming I get some user input asking
for a number, how would I spawn 'n' objects from a class?
i.e. I have a class class 'user' and I don't know how many of them I
want to spawn.
Any ideas?
Sure. This will give you a list of n instances of user:

[user() for i in range(n)]

Of course, you could also use a good old for loop:

for i in range(n):
u = user()
...do something with u...

Hope this helps!

--
Hans Nowak (zephyrfalcon at gmail dot com)http://4.flowsnake.org/

whoops, replied to author....

What I wanted to ask before was won't 'u' be overwritten with a new
object each time the loop ticks over?
Yes, so you have to store it somewhere, if you want to keep the object around.
The list comprehension mentioned above stores all the objects in a list, after
which they can be accessed at will via indexing.
what I want to do is have, say 5 users in a game, so I'd have to spawn
5 objects. I can't do that because I have'nt hardcoded any object
names for them.

or does it somehow work? how would I address them if they all have the
name 'u'?
users = [user() for i in range(n)]

# use: users[0], users[1], etc

--
Hans Nowak (zephyrfalcon at gmail dot com)
http://4.flowsnake.org/
Jun 27 '08 #8
On Jun 8, 2:58 am, Hans Nowak <zephyrfalcon!NO_SP...@gmail.comwrote:
Kalibr wrote:
On Jun 7, 1:20 pm, Hans Nowak <zephyrfalcon!NO_SP...@gmail.comwrote:
Kalibr wrote:
I've been developing a small script to fiddle with classes, and came
accross the following problem. Assuming I get some user input asking
for a number, how would I spawn 'n' objects from a class?
i.e. I have a class class 'user' and I don't know how many of them I
want to spawn.
Any ideas?
Sure. This will give you a list of n instances of user:
[user() for i in range(n)]
Of course, you could also use a good old for loop:
for i in range(n):
u = user()
...do something with u...
Hope this helps!
--
Hans Nowak (zephyrfalcon at gmail dot com)http://4.flowsnake.org/
whoops, replied to author....
What I wanted to ask before was won't 'u' be overwritten with a new
object each time the loop ticks over?

Yes, so you have to store it somewhere, if you want to keep the object around.
The list comprehension mentioned above stores all the objects in a list, after
which they can be accessed at will via indexing.
what I want to do is have, say 5 users in a game, so I'd have to spawn
5 objects. I can't do that because I have'nt hardcoded any object
names for them.
or does it somehow work? how would I address them if they all have the
name 'u'?

users = [user() for i in range(n)]

# use: users[0], users[1], etc

--
Hans Nowak (zephyrfalcon at gmail dot com)http://4.flowsnake.org/
Ok, wait, I see where this is going.
I just did the list comprehension.
I was under some misguided idea that you actually had to have a unique
variable name for all the new objects you spawned. Thanks for all you
help guys!
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: VK | last post by:
09/30/03 Phil Powell posted his "Radio buttons do not appear checked" question. This question led to a long discussion about the naming rules applying to variables, objects, methods and properties...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
2
by: Thomas W. Brown | last post by:
If I am using the CSharpCodeProvider to dynamically compile an in-memory assembly from some C# source, do I need to worry about signing this assembly if I'm doing the compilation, instantiation,...
1
by: | last post by:
In Actionscript 2.0 there is the concept of dynamically generating and naming objects at runtime. Dynamically named objects can be referenced and manipulated programmatically using array-access...
14
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them......
6
by: dm1608 | last post by:
I'm relatively new to ASP.NET 2.0 and am struggling with trying to find the best naming convention for the BAL and DAL objects within my database. Does anyone have any recommendations or best...
3
by: rsine | last post by:
I have searched around a little and have yet to find a naming convention for the string builder object. I really do not want to use "str" since this is for string objects and thus could be...
35
by: Smithers | last post by:
Is it common practise to begin the name of form classes with "frm" (e.g., frmOneForm, frmAnotherForm). Or is that generally considered an outdated convention? If not "frm" what is a common or...
6
by: RSH | last post by:
Hi, i have a situation where I need to dynamically create objects in a loop. My question surrounds intantiation naming in such a scenerio. Below is a snippet that is basically hardcoding each...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.