Python callable method


The callable() function is used to check if an object is callable. If True is returned, the object may still fail to be called; but if False is returned, the object ojbect will never be called.

It returns True for functions, methods, lambda functions, classes, and instances of classes that implement the call method.

Related course: Complete Python Programming Course & Exercises

The Python callable() Method

The synax is:

callable(object)

parameters: object -- object return value: boolean return value, True or False

Python callable example

You can call the callable() method directly from the Python shell, on anything.

For simple date like an integer or string it returns False, because you can't run a variable.

>>>callable(0)
False
>>> callable("example")
False

But for a function callable returns True.

>>> def add(a, b):
...     return a + b
... 
>>> callable(add)
True

What about classes?

>>> class A:
...     def method(self):
...             return 0
... 
>>> callable(A)
True

Yes, for class methods it returns True. A class method is callable.

>>> a = A()
>>> callable(a)
False

But the object created from a class is not.

>>> class B:
...     def __call__(self):
...             return 0
... 
>>> callable(B)
True
>>> b = B()
>>> callable(b)
True

python callable method

You can also call the callable() function inside a Python script.

print(callable(0)) #False
print(callable([0,1])) #False
print(callable({0:1,0:"age"})) #False

def multiply(a,b):
    return a*b;
print(callable(multiply))

class B:
    def method(self):
        return
print(callable(B))

b = B()
print(callable(b))

class C:
    def __call__(self, *args, **kwargs):
        return
print(callable(C))

c = C()
print(callable(c))