call of non-function (type string) error

Michael Hudson mwh21 at cam.ac.uk
Wed Apr 11 13:16:48 EDT 2001


Andrew Kuchling <akuchlin at mems-exchange.org> writes:

> Graham Guttocks <graham_guttocks at yahoo.co.nz> writes:
> > >>> CIPHER = "DES3"
> > >>> import Crypto.Cipher
> > >>> cipher = getattr(Crypto.Cipher, CIPHER)
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in ?
> > AttributeError: DES3
> 
> I don't think the Cipher package automatically imports all
> subpackages, and this is a case where you really do need to use exec.

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

I don't have the Crypto package, but:

>>> import distutils
>>> dir(distutils)
['__builtins__', '__doc__', '__file__', '__name__', '__path__', 
 '__revision__', '__version__']
>>> __import__("distutils.file_util",globals(),{},[1])
<module 'distutils.file_util' from '/usr/lib/python2.0/distutils/file_util.pyc'>
>>> dir(distutils)
['__builtins__', '__doc__', '__file__', '__name__', '__path__', 
 '__revision__', '__version__', 'errors', 'file_util']

> Obviously the Cipher package should provide a function to retrieve the
> module for a given name...

This is probably easier!

Cheers,
M.

-- 
  ... when all the programmes on all the channels actually were made
  by actors with cleft pallettes speaking lines by dyslexic writers
  filmed by blind cameramen instead of merely seeming like that, it
  somehow made the whole thing more worthwhile.   -- HHGTG, Episode 11



More information about the Python-list mailing list