newbie question: how to get the class instance given a moduleobject?

Kent Johnson kent37 at tds.net
Mon Mar 28 16:51:28 EST 2005


Steven Bethard wrote:
> Tian wrote:
> 
>> import ModuleA
>>
>> classname = "Dog"
>> module = globals()["ModuleA"]
>> classobj = ???????           <---using classname
>> instanct = classobj()
> 
> 
> classobj = getattr(module, classname)

There may not be any need for the indirect lookup of ModuleA, you can say
classobj = getattr(ModuleA, classname)

Kent



More information about the Python-list mailing list