Dynamically naming objects.

K

Kalibr

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?
 
H

Hans Nowak

Kalibr said:
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!
 
K

Kalibr

Kalibr said:
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!

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'?
 
A

Alan Isaac

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
 
S

stefaan.himpe

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.
 
K

Kalibr

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.
 
H

Hans Nowak

Kalibr said:
Kalibr said:
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!

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
 
K

Kalibr

Kalibr said:
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!
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

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!
 

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,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top