callable() builtin-function

Peter Hansen peter at engcorp.com
Wed Feb 19 19:47:19 EST 2003


Andrew Bennetts wrote:
> 
> On Wed, Feb 19, 2003 at 07:09:37PM -0500, Peter Hansen wrote:
> > Andrew Bennetts wrote:
> > >
> > > When it's probably more Pythonic to do:
> > >
> > >     try:
> > >         x = foo()
> > >     except TypeError:
> > >         x = foo
> > >
> > Of course, there is one potential problem with this, although the
> > fix for it is not necessarily callable().  The problem is if
> > the routine foo() really exists and is callable, but somewhere
> > inside has a failure which raises a TypeError.  This might
> > then be interpreted incorrectly.
> 
> 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
can't tell the difference unless you explicitly check the top
level first (though of course that gets you almost nowhere...
the call could easily fail for some other reason, so who cares
if it was really callable if it fails anyway).

-Peter





More information about the Python-list mailing list