call of non-function (type string) error

Ken Seehof kens at sightreader.com
Tue Apr 10 21:09:35 EDT 2001


----- Original Message ----- 
From: "Graham Guttocks" <graham_guttocks at yahoo.co.nz>
To: <python-list at python.org>
Sent: Tuesday, April 10, 2001 5:23 PM
Subject: call of non-function (type string) error


> Greetings,
> 
> Any suggestions on how to get around this problem?  I'm trying to call
> a function using variables, but it obviously doesn't work that way.
> 
> -----------------------------------------------
> 
> CIPHER = "DES3"
> exec "from Crypto.Cipher import %s" % CIPHER
> 
> # cipherobj = DES3.new(KEY, DES3.CBC, IV)
> cipherobj = CIPHER + 'new'(KEY,CIPHER + '.CBC',IV)
> -----------------------------------------------
> 
> 
> Traceback (most recent call last):
>   File "<stdin>", line 10, in ?
> TypeError: call of non-function (type string)

I'm having almost as much trouble as python trying
to figure out your syntax :-)   I'm not familiar with the
Crypto module.

I'm sure someone will give you a correct anwer here
but you probably should practice using eval and exec
until you know -exactly- what they do.  Only practice
will help you understand how strings and the python
interpreter interact, and the difference between a string
literal and a named object or expression.  Start with
small examples where there are few unknowns.

Here's what doesn't work in your code:

... CIPHER + 'new'(...)

CIPHER is a string and 'new' is a string, so
CIPHER + 'new' is a string with value "DES3new".
You can't invoke a string as if it were a function,
(and anyway you are missing a ".").

Change it to ... eval(CIPHER+'.new')(...)

... (..., CIPHER + '.CBC', ...)

CIPHER+'.CBC' is a string equal to "DES3.CBC".
The function wants the value of the expression DES3.CBC.
A string is not the same as the value of it's expression.
That's why we have the eval() function.

Change it to ... (..., eval(CIPHER + '.CBC'), ...)

... ( ..., IV)

I don't know what IV is.

A better approach to this whole thing is this:


import Crypto.Cipher

CIPHER = "DES3"
ciphertype = getattr(Crypto.Cipher, CIPHER)
cipherobj = ciphertype.new(KEY, ciphertype.CBC, IV)


Have fun...

- Ken Seehof
kseehof at neuralintegrator.com
www.neuralintegrator.com






More information about the Python-list mailing list