dynamic class instantiation
kent at kentsjohnson.com
Mon Jan 30 18:18:36 EST 2006
Ognen Duzlevski wrote:
> I have a parser that will go through the language definition
> file and produce the following as a separate .py file:
> class page(object):
> def __init__():
> self.name = None
> self.caption = None
> self.functions = 
> Say I got "page" as a string. How do I go about
> instantiating a class from this piece of information? To make it
> more obvious how do I create the page() class based on the "page"
> string I have?
If the definition of page is in my_objects.py, and that file is on the
Python search path, you can use
cls = getattr(my_module, 'path')
instance = cls()
to create an instance of my_module.path.
More information about the Python-list