Created
January 27, 2014 20:33
-
-
Save kgriffs/8656713 to your computer and use it in GitHub Desktop.
Python: Callable behavior on new and old-style classes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [6]: callable? | |
Type: builtin_function_or_method | |
String Form:<built-in function callable> | |
Namespace: Python builtin | |
Docstring: | |
callable(object) -> bool | |
Return whether the object is callable (i.e., some kind of function). | |
Note that classes are callable, as are instances with a __call__() method. | |
In [7]: class Foo(object): | |
...: pass | |
...: | |
In [8]: callable(Foo) | |
Out[8]: True | |
In [9]: hasattr(Foo, '__call__') | |
Out[9]: True | |
In [10]: foo = Foo() | |
In [11]: callable(foo) | |
Out[11]: False | |
In [12]: hasattr(foo, '__call__') | |
Out[12]: False | |
In [13]: %timeit hasattr(foo, '__call__') | |
1000000 loops, best of 3: 579 ns per loop | |
In [14]: %timeit callable(foo) | |
10000000 loops, best of 3: 57.3 ns per loop | |
In [15]: class OldFoo: | |
....: pass | |
....: | |
In [16]: callable(OldFoo) | |
Out[16]: True | |
In [17]: hasattr(OldFoo, '__call__') | |
Out[17]: False | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment