A newbie in more need....
Peter L Hansen
peter at engcorp.com
Fri Oct 1 14:55:15 CEST 2004
Chris Patton wrote:
> 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):
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?
More information about the Python-list