A newbie in more need....

C

Chris Patton

I am writing a program that writes a number of classes, which are all
fundementally the same thing. The only thing that is different is the
names. Therefore, I need to write a bunch of classes in the quickest
time I can. Here's what I was thinking of:

num = 1
while num < 20:
exec 'class bug'+num+':'
exec ' gender = "m"'
exec ' size = 0'
exec ' skill = 0'

Obviously, the "exec" statement severley slows the time it takes to
write these classes. I need a new method! If possible I would like to
avoid the use of the "exec" statement alltogether.

-- Thanks for any help!!
 
P

Paul Rubin

I am writing a program that writes a number of classes, which are all
fundementally the same thing. The only thing that is different is the
names. Therefore, I need to write a bunch of classes in the quickest
time I can. Here's what I was thinking of: ...

Rather than ask the best way do that, ask "why in the heck would
anyone want to do such a bizarre thing under any circumstances"?
If you say what your application is actually trying to accomplish
with those classes, that might get some better answers.
 
P

Peter L Hansen

Chris said:
I am writing a program that writes a number of classes, which are all
fundementally the same thing. The only thing that is different is the
names. Therefore, I need to write a bunch of classes in the quickest
time I can. Here's what I was thinking of:

num = 1
while num < 20:
exec 'class bug'+num+':'
exec ' gender = "m"'
exec ' size = 0'
exec ' skill = 0'

Obviously, the "exec" statement severley slows the time it takes to
write these classes. I need a new method! If possible I would like to
avoid the use of the "exec" statement alltogether.

for i in range(1, 20):
class _proto:
gender = "m"
size = 0
skill = 0

globals()['bug%s' % i] = _proto

This does almost exactly what you want, and for most
intents and purposes is the same thing. (The encoded
name of the class is different, but that shouldn't
be relevant to you in most cases.)

The real question has already been asked by others,
however: what's the point of auto-generating names for
things when nothing will be able to refer to them without
knowing their names in advance anyway?

-Peter
 
L

Lonnie Princehouse

The fumanchu code is indeed the excellent way to do what is asked, but
it really looks like instances would be more appropriate for your
particular application! Why do you want a bunch of identical classes
that differ in name only?

Why not:

class Bug(object):
def __init__(self, gender='m', size=0, skill=0):
self.gender = gender
self.size = size
self.skill = skill

# A list of bugs
bugs = [Bug() for i in xrange(N)]

# or, if you insist on cluttering your namespace,
for i in xrange(N):
globals()['bug%s' % i] = Bug()
 
B

Bengt Richter

Why didn't you _try_ your own code, Chris? It wouldn't have taken any more typing ;-)
I guess you did avoid it, since otherwise you would at least have discovered
... exec 'class bug'+num+':'
... exec ' gender = "m"'
... exec ' size = 0'
... exec ' skill = 0'
...
Traceback (most recent call last):
File "<stdin>", line 2, in ?
TypeError: cannot concatenate 'str' and 'int' objects

-- for starters. If you don't do your part, you can expect people to lose interest
in helping you. Another pointer: If you are new to town, you can ask an old taxi driver
how certain streets connect, and he can answer, but if you have an address to go to, tell
him that. You'll generally get there faster ;-)
for i in range(1, 20):
class _proto:
gender = "m"
size = 0
skill = 0

globals()['bug%s' % i] = _proto

This does almost exactly what you want, and for most
intents and purposes is the same thing. (The encoded
name of the class is different, but that shouldn't
be relevant to you in most cases.)
If it is, type is handy:
... name = 'bug%s'%i
... globals()[name]=type(name,(),dict(gender='m', size=0, skill=0))
...

Have a look at one ('m', 0, 0)
The real question has already been asked by others,
however: what's the point of auto-generating names for
things when nothing will be able to refer to them without
knowing their names in advance anyway?
Well, I guess there could be reasons, but guessing gets old sometimes ;-/

Regards,
Bengt Richter
 

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
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top