newbie question - object from a string?

Kragen Sitaker kragen at pobox.com
Tue Nov 27 23:14:12 CET 2001


Gordon Scott <gordon.scott at peregrine.com> writes:
> I have a file named MyHandlers.py in which I have the class MyHandler.
> 
> when I use
> 
> __import__('MyHandlers', globals(), locals(), ['MyHandler'])  or
> .
> .
> .
> eval('MyHandler()', globals(), locals())  or eval('MyHandlers.MyHandler()',
> globals(), locals())
> 
> I get a NameError: name 'MyHandler' is not defined.

__import__ doesn't import anything into your namespace; it just loads
and returns the module.  You can say:

MyHandler = __import__('MyHandlers').MyHandler

and then you can call MyHandler.

HTH.




More information about the Python-list mailing list