A newbie in more need....
steve at holdenweb.com
Fri Oct 1 07:18:24 CEST 2004
Robert Brewer wrote:
> 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.
[excellent fumanchu code]
having said that, usually when somebody suggests they need to
dynamically create names in this way, it's often an indication that the
created objects should be kept in a dictionary.
How are you going to look these things up? What will be making the
creator calls to bug1(), bug2(), and so on, and why couldn't it make
calls to bug(), bug(), and so on?
A little more detail might get you an even better solution.
More information about the Python-list