A newbie in more need....

Lonnie Princehouse finite.automaton at gmail.com
Fri Oct 1 16:22:13 EDT 2004


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()



chrispatton at gmail.com (Chris Patton) wrote in message news:<9c3edc58.0409302034.54f93dd2 at posting.google.com>...
> 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!!



More information about the Python-list mailing list