call of non-function (type string) error

Carey Evans careye at spamcop.net
Thu Apr 12 07:03:50 EDT 2001


Michael Hudson <mwh21 at cam.ac.uk> writes:

> Why wouldn't __import__("Crypto.Cipher.%s"%(CIPHER,),globals(),{},[1])
> followed by cipher = getattr(Crypto.Cipher, CIPHER) work?

To get the module for CIPHER imported properly, you just need to pass
its name in the fourth parameter to __import__:

>>> CIPHER = 'DES3'
>>> pkg = __import__('Crypto.Cipher', globals(), locals(), [CIPHER])
>>> module = getattr(pkg, CIPHER)
>>> print module
<module 'Crypto.Cipher.DES3' from '/home/carey/lib/python/Crypto/Cipher/DES3.pyc'>

No need to use exec, eval or %s anywhere.

keeping-the-world-free-of-exec-ly y'rs   - Carey

-- 
	    "Quiet, you'll miss the humorous conclusion."



More information about the Python-list mailing list