[issue13258] replace hasattr(obj, '__call__') with callable(obj)

Florent Xicluna report at bugs.python.org
Mon Oct 24 21:46:15 CEST 2011


New submission from Florent Xicluna <florent.xicluna at gmail.com>:

Now that callable() is back in 3.2, we may replace hasattr(obj, '__call__') with callable(obj).

The built-in function is easier to read and gives better performance than attribute lookup.


$ ./python -m timeit "hasattr(None, '__call__')"
100000 loops, best of 3: 4.09 usec per loop
$ ./python -m timeit "hasattr(str, '__call__')"
1000000 loops, best of 3: 1.3 usec per loop

$ ./python -m timeit "callable(None)"
1000000 loops, best of 3: 0.299 usec per loop
$ ./python -m timeit "callable(str)"
1000000 loops, best of 3: 0.369 usec per loop

----------
components: Library (Lib)
messages: 146320
nosy: flox
priority: normal
severity: normal
status: open
title: replace hasattr(obj, '__call__') with callable(obj)
type: performance
versions: Python 3.2, Python 3.3

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


More information about the Python-bugs-list mailing list