pythoncallable

Check if an object is callable


I want to find out if an object is callable or not.

I know that type() would return <class 'method'>. But I don't know how I can check for that (e.g. with isinstance()).


Solution

  • Use the builtin callable():

    >>> callable(open)
    True
    >>> print(callable.__doc__)
    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.
    

    It's a great investment making an effort to master at least 90% of the builtin functions listed here: https://docs.python.org/3/library/functions.html