A newbie in more need....
Steve Holden
steve at holdenweb.com
Fri Oct 1 01:18:24 EDT 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[1](), bug[2](), and so on?
A little more detail might get you an even better solution.
regards
Steve
More information about the Python-list
mailing list