[issue23990] Callable builtin doesn't respect descriptors

Christian Heimes report at bugs.python.org
Fri Apr 17 22:14:45 CEST 2015


Christian Heimes added the comment:

I have closed the issue because the code behaves according to the language specs and the language design. It is not broken at all. The callable test just checks for the attribute __call__ on the *type* of an object. The check is not performed on the *object* itself.

In your example

  callable(a)

does not do

  hasattr(a, '__call__')

but

  hasattr(type(a), '__call__')

which translates to

  hasattr(A, '__call__')


The behavior is very well consistent. I presume it just doesn't match your expectations. Special methods have a slightly different lookup behavior than ordinary. Due to the highly dynamic nature of Python the __call__ attribute is not validated at all.

For example this is expected behavior:

>>> class Example:
...     __call__ = None
... 
>>> callable(Example())
True
>>> class Example:
...     __call__ = None
... 
>>> example = Example()
>>> callable(example)
True
>>> example()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23990>
_______________________________________


More information about the Python-bugs-list mailing list