[Python-3000] callable()

Fernando Perez fperez.net at gmail.com
Wed Jul 19 08:47:12 CEST 2006


Greg Ewing wrote:

> For that purpose, hasattr(obj, '__call__') is sufficient,
> I think. And if it's not, the API you're trying to
> implement is ill-conceived in the first place, just like
> testing for sequenceness is not a well-defined operation.

I should add that for ipython's purposes, it seems I could just implement

def callable(o): return hasattr(o,'__call__')

and it would be OK.  I hadn't realized that Python reports true to the
hasattr() call form for lambdas, simple plain functions, functions in
extension modules, extension types, and most things I could quickly think
of and check from code I have.  I wasn't aware of this fact, I thought it
would only be true for instances of classes which explicitly implemented a
__call__ method, and that callable() did some other magic.

Having understood this, I then don't really care whether callable() goes
away or not, it's a one-liner as an interactive ipython convenience.  Sorry
for not checking more carefully, I trusted a misconception I had.

Cheers,

f



More information about the Python-3000 mailing list