fetching class by name

Greg Ewing greg at cosc.canterbury.ac.nz
Tue Feb 13 19:18:10 EST 2001


Someone whose attribution has been lost said:

> >I think maybe I'm just spoiled by the way objective c gives you a
> >cute little utility function to do this for you. Still, looking
> >it up manually seems extraordinarily costly.

All the class names live in a single namespace in Objective C.
In Python the situation is more complicated. If you have no idea
which module the class is defined in, then you've no choice but
to go searching.

On the other hand, if you know both the module name and the 
class name, then you can go straight to it:

  import sys
  c = getattr(sys.modules[module_name], class_name)

-- 
Greg Ewing, Computer Science Dept, University of Canterbury,	  
Christchurch, New Zealand
To get my email address, please visit my web page:	  
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list