dynamic class/module use? (like Java's forName)

Robert Brewer fumanchu at amor.org
Sat Jul 17 22:44:05 CEST 2004


Alex Hunsley wrote:
> > Does python provide a way to dynamically use modules and/or classes?
> > I'm thinking in the vein of Java's Class.forName.
> > 
> > As a pseudocode example, I'm looking for the following ability:
> > 
> > 
> > classIWantToInstantiate = "packagenamehere.classNameHere"
> > 
> > anInstanceOfThatClass = 
> makeNewClassFromString(classIWantToInstantiate, 
> > parameters[])
> > 
> > # now anInstanceOfThatClass refers to an instantied
> > # packagenamehere.classNameHere object!
> > 
> Oops.. I googled but not hard enough obviously! I found my answer on 
> google groups:
> 
> http://shorterlink.com/?N2HPGU

I have a more complete version of this functionality available:

http://www.aminus.org/rbre/python/xray.py

Feel free to drop it in your site-packages directory. :)


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list