Python OOP Problem

Lie Ryan lie.1296 at gmail.com
Tue Dec 29 16:10:26 EST 2009


On 12/29/2009 3:01 PM, Миклухо wrote:
> On 28 дек, 18:29, "Martin v. Loewis"<mar... at v.loewis.de>  wrote:
>> In this case (you just started to learn Python), I recommend to take
>> an explicit approach. Create a dictionary that maps class names to
>> classes:
>>
>> name2class = { "MyObject" : MyObject,
>>                 "MyOtherObject" : MyOtherObject,
>>                 "Etc" : Etc }
>>
>> Then, when you receive the string class_name, you do
>>
>> o = name2class[class_name]
>> o.myfunction()
>>
>> HTH,
>> Martin
>
> Thanks for reply, but it doesn't fit to my task. If I will add later
> other objects(and it will be very often) - I should stop the service,
> but that would be very bad.

you don't need to stop any service; just add into the dictionary:
name2class['NewObject'] = NewObject

> I'm not sure, if this is solution, but test passed:
>
> myimportmod = __import__('ClassName', globals(), locals(),
> ['ClassName'], -1)
> mod = getattr(_myimportmod, 'ClassName')
> o = mod();
> o.myfunction();




More information about the Python-list mailing list