dynamic class/module use? (like Java's forName)
Alex Hunsley
lard at tardis.ed.ac.molar.uk
Sat Jul 17 22:32:33 EDT 2004
Robert Brewer wrote:
> 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
ah, thanks! looks like it could be handy!
Your source code comment has hit the nail on the head - I am looking at
doing the strategy pattern in a modular way. (Where each 'action' in my
project is some code that matches a given interfaces).
I'm working on a GPL licensed project, not sure what licensing your
above code is based on.
ta,
alex
More information about the Python-list
mailing list