<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
  <title></title>
</head>
<body>
My apologies. Didn't notice the reply from Alex Martelli, which already answers
the following question. Now, I am able to do entirely what I describe below
using __import__ followed by class and object instantiation.<br>
<br>
Thanks a lot to all who replied.<br>
<br>
\vivek<br>
<br>
Vivek Sawant wrote:<br>
<blockquote type="cite" cite="mid3E6155F3.4040608@verizon.net">     
  <meta http-equiv="Content-Type" content="text/html;">
  <title></title>
     Lenard,<br>
 <br>
 Thanks!! That worked. I need the first method of creating class and object 
instances as I am not creating class definitions on the fly.<br>
 <br>
 Another question though... To be able to use the method suggested by you 
(<font face="Courier New, Courier, monospace">obj = eval ('<classname>'</font>), 
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.<br>
 <br>
 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.<br>
 <br>
 I (naively) tried <b><font face="Courier New, Courier, monospace"
 color="#006600">eval ('import <module>')</font></b>. That produced 
syntax error :-(<br>
 Any suggestions?<br>
 <br>
 \vivek<br>
 <br>
 Lenard Lindstrom wrote:<br>
 
  <blockquote type="cite"
 cite="midmac8a.9911$E94.247698@news0.telusplanet.net">   
    <pre wrap="">"Vivek Sawant" <a class="moz-txt-link-rfc2396E"
 href="mailto:vivek-sawant@verizon.net"><vivek-sawant@verizon.net></a> wrote in message
<a class="moz-txt-link-freetext"
 href="news:3E611ABE.2050102@verizon.net">news:3E611ABE.2050102@verizon.net</a>...
  </pre>
   
    <blockquote type="cite">     
      <pre wrap="">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.

    </pre>
   </blockquote>
   
    <pre wrap=""><!---->
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()

  </pre>
   
    <blockquote type="cite">     
      <pre wrap="">For example, in Java you can create a 'Class' object as:

Class.forname ('<classname>')

Thanks.

\vivek

    </pre>
   </blockquote>
   
    <pre wrap=""><!---->
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





  </pre>
 </blockquote>
 <br>
 </blockquote>
<br>
</body>
</html>