other ways to check for <type 'function'>?

Christophe chris.cavalaria at free.fr
Thu Nov 2 16:59:56 CET 2006


Christophe a écrit :
> Sybren Stuvel a écrit :
>> Christophe enlightened us with:
>>> I don't think it's a good idea because when you place a try catch
>>> block around a function call, you'll catch any exception thrown by
>>> the function itself and not only the "cannot be called" exception.
>>
>> That depends on the exception you're catching, doesn't it?
> 
> And what if the function has an error and calls a non callable object ?
Or even worse. Since we have :

 >>> None()
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
TypeError: 'NoneType' object is not callable
 >>> 1+""
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for +: 'int' and 'str'
 >>> ""+1
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
TypeError: cannot concatenate 'str' and 'int' objects

All of them are TypeError exceptions. It is extremly dangerous to put a 
try except TypeError clause around a function call to catch the case 
where the function object isn't a callable :/



More information about the Python-list mailing list