Re: dynamic class instantiation
- From: Kent Johnson <kent@xxxxxxxxxxxxxxxx>
- Date: Mon, 30 Jan 2006 18:18:36 -0500
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?
Use getattr().
If the definition of page is in my_objects.py, and that file is on the Python search path, you can use
import my_module
cls = getattr(my_module, 'path')
instance = cls()
to create an instance of my_module.path.
Kent .
- Follow-Ups:
- Re: dynamic class instantiation
- From: Ognen Duzlevski
- Re: dynamic class instantiation
- References:
- dynamic class instantiation
- From: Ognen Duzlevski
- dynamic class instantiation
- Prev by Date: Re: simple perl program in python gives errors
- Next by Date: Marked-Up Text Viewer for Python/Tkinter
- Previous by thread: Re: dynamic class instantiation
- Next by thread: Re: dynamic class instantiation
- Index(es):