How to check if the instances of a given class are callable? This is easy to do if you instantiate the class and then use callable()
. But my question is how to check this without instantiating. Take for example the Calendar
class:
>>> import calendar
>>> callable(calendar.Calendar())
False
I want to do the same but without instantiating, i.e. implement some function callable_class_instances()
such that:
>>> import calendar
>>> callable_class_instances(calendar.Calendar)
False
>>>
>>> class MyFunc:
... def __init__(self):
... print('Should not run on callable_class_instances call.')
... def __call__(self):
... print('MyFunc instance called.')
>>> callable_class_instances(MyFunc)
True
Is there any simple way to do this which does not look like a hack?
From the docs:
The problem with my previous attempt (
hasattr
) was that all classes have a__call__
method so that we can initialise them. This should work most of the time:Note that this will not work for non-Python
__call__
implementations.