Can you create a class from a string name

Vivek Sawant vivek-sawant at verizon.net
Sun Mar 2 01:58:16 CET 2003


Lenard,

Thanks!! That worked. I need the first method of creating class and 
object instances as I am not creating class definitions on the fly.

Another question though... To be able to use the method suggested by you 
(obj = eval ('<classname>'), I need to have already imported the class 
that I am trying to instantiate. Here's what I am really trying to do. I 
propose to support an applicaiton-specific configuration file which, 
along with other app-specific parameters, will also contain the names of 
the classes to be instantiated. This will enable the users of this 
application to use alternate implementations of a class by just changing 
the config file.

Given this, the '<classname>' string will come from this config file at 
the runtime. It would be nice, if the module that parses the config file 
does not have to know which all classes to import when I write the module.

I (naively) tried eval ('import <module>'). That produced syntax error :-(
Any suggestions?

\vivek

Lenard Lindstrom wrote:

>"Vivek Sawant" <vivek-sawant at verizon.net> wrote in message
>news:3E611ABE.2050102 at verizon.net...
>  
>
>>Hi,
>>
>>is there a way to create a class object or an instance object for a
>>class if you have the name of the class as a string at the runtime.
>>
>>    
>>
>
>You can use Python's dynamic execution features to do what you want. To
>retrieve a reference to an existing class object use the built-in 'eval'
>function.
>
>classobj = eval('<classname>')
>
>Create an instance of the class by calling the returned object.
>
>classinstance = classobj(<constructor args>)
>
>To define an entirely new class at run-time use the 'exec' statement. The
>following example creates a minimal class, but the definition can be as
>complicated as you need.
>
># Class name is defined by a string.
>classname = 'mynewclass'
>
># The class definition is also a string. The '%s' will be replaced by the
>class name later.
># When writing a compound statement as a string make sure the indentation
>method
># is consistent, e.g. four spaces. See section 2.4.1 in Python Language
>Manual
># for more on triple-quoted strings.
>definition = """class %s:
>    pass
>"""
>
># Execute the class definition statement. Insert the class name using a
># string formatting command '%' (section 2.2.6.2 in Python Library
>Reference).
>exec definition % classname
>
># Create an instance of the class.
>newinstance = eval(classname)()
>
># This also works, but only when classname == 'mynewclass'.
>newinstance = mynewclass()
>
>  
>
>>For example, in Java you can create a 'Class' object as:
>>
>>Class.forname ('<classname>')
>>
>>Thanks.
>>
>>\vivek
>>
>>    
>>
>
>The Java Class.forName method returns a reference to an already existing
>'Class' object. It does not create a new 'Class' object. It will not create
>a new class at run time.
>
>I hope this helps.
>
>Lenard Lindstrom
>
>
>
>
>
>  
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20030302/bf294f97/attachment.html>


More information about the Python-list mailing list