classobj?

attn.steven.kuo at gmail.com attn.steven.kuo at gmail.com
Mon Feb 26 21:19:20 EST 2007


On Feb 26, 5:43 pm, Venky <v... at nospam.com> wrote:
> Hi,
>
> I am trying to create classes at runtime based on input from a textfile.
> I am trying to use the function new.classobj. I am able to create the
> new classes successfully, however I fail to understand on how to add
> this new class to the current dictionary.
>
> cl = new.classobj('SomeClass', (BaseClass, ), {})
>
> After this call, how do I instantiate SomeClass?
>
> I understand cl() will instantiate this, however this defeats my
> purpose, since the name of the class is obtained at runtime.
>


Do you mean that you want to add it to globals()?

globals()['SomeClass'] = cl

myinst = SomeClass()
print isinstance(myinst, SomeClass)
print isinstance(myinst, BaseClass)

--
Hope this helps,
Steven





More information about the Python-list mailing list