callable() builtin-function

Greg Ewing (using news.cis.dfn.de) me at privacy.net
Thu Feb 20 22:37:44 EST 2003


Andrew Bennetts wrote:
> On Wed, Feb 19, 2003 at 07:47:19PM -0500, Peter Hansen wrote:
> 
>>Andrew Bennetts wrote:
>>
>>>Hence my comment about preferring to have a NotCallableError.  :)
>>
>>I'm pretty sure that wouldn't solve the problem.  Think about it --
>>the NotCallableError might have been raised by another attempted
>>function call *within* the successful call to foo().  You
> 
> 
> Of course, but the same applies to all exceptions.

That's quite true, which is why I'm always nervous about
catching what one might call "error-reporting exceptions"
such as TypeError, IndexError, etc. that are very likely
to result from bugs. I prefer to use operations like
x.get(y) etc. where they are available, rather than
catching exceptions.

In this case, there isn't any atomic try-it-and-tell-
me-if-it-failed way of calling something, so you either
have to look-before-you-leap or risk using a try-except
that could catch too much.

My suggestion would be to re-design things so that you
can assume that foo is *always* callable. Then the
problem doesn't arise.

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg





More information about the Python-list mailing list