dinamical object creation

Courageous jkraska1 at san.rr.com
Mon May 8 22:16:40 EDT 2000


martin_valiente at my-deja.com wrote:
> 
> Hi *
> 
> How can I create an object at runtime, giving its module and class name?
> In java it would be:

A partial answer, without the module capability, using a different solution
than the last poster:

o=eval(className)()

In the other language that I'm familiar with, "eval" is expensive, so
I'm pretty sure the other poster's answer is better. But I include
this here for instructive purposes. Python eval() can be used to
interpret any valid python expression which is currently in the form
of a string. You probably should only make very judicious use of this
function, but still...

Pretty nifty, huh?

:)-


C/



More information about the Python-list mailing list