callable() builtin-function

Andrew Bennetts andrew-pythonlist at puzzling.org
Wed Feb 19 19:05:59 EST 2003


On Wed, Feb 19, 2003 at 03:53:25PM +0100, Gerrit Holl wrote:
> Hi,
> 
> In the Python regrets list, at http://www.python.org/doc/essays/ppt/,
> Although I can understand most of the regrets put there, I do not
> understand why Guido has put the callable() function there, with the
> comment "just call it".  AFAIK, callable(x) equals hasattr(x,
> '__call__'), but what is wrong with callable()? Why could it be
> something to regret? Can it be heavily abused? Is it ugly? Is it bad
> style?

I suspect it's because it encourages code like:

    if callable(foo):
        x = foo()
    else:
        x = foo

When it's probably more Pythonic to do:

    try:
        x = foo()
    except TypeError:
        x = foo

[Looking at that, I'd feel slightly more comfortable catching a
"NotCallableError" (which would subclass TypeError), but it's not a big
issue.]

-Andrew.






More information about the Python-list mailing list