Contents
Python – callable()
Python callable() builtin function is used to check if given argument is callable or not.
callable() returns a boolean value of True if the given argument is callable, or False if not.
In this tutorial, you will learn the syntax of callable() function, and then its usage with the help of example programs.
Syntax
The syntax of callable() function is
callable(object)
where
Parameter | Description |
---|---|
object | Any Python object. |
Examples
1. Check if user defined function is callable
In the following program, we define a function sayHello(), and programmatically check if this function object is callable using callable() builtin function. Since, a function is callable, callable(sayHello) must return True.
Python Program
def sayHello():
print('hello user')
output = callable(sayHello)
print(f'callable() : {output}')
Run Code CopyOutput
callable() : True
2. Check if integer value is callable
An integer value is not a callable object. Therefore, callable() with integer passed as argument returns False.
Output
callable() : False
3. Check if built-in function is callable
print() is an inbuilt function and callable. Therefore, print as an argument to callable() returns True.
Output
callable() : True
4. Check if lambda function is callable
Lambda function is a callable function. callable() with lambda function passed as argument must return True.
Python Program
sayHello = lambda : print('hello user')
output = callable(sayHello)
print(f'callable() : {output}')
Run Code CopyOutput
callable() : True
Summary
In this tutorial of Python Examples, we learned the syntax of callable() function, and how to check if given object is callable or not, with examples.