The callable()
function is used in Python to determine if an object is callable. In other words, it checks if the object can be called like a function. In this article, I will tell you what the python callable() function syntax is, how to use it with some examples.
1. Python callable() Function Definition.
callable()
is a built-in Python function that returnsTrue
if the specified object is callable (i.e., can be called like a function) andFalse
otherwise.- In other words,
callable()
checks whether an object is a function or a variable. - The syntax for
callable()
function is as follows:callable(object)
- Here, object is the object that we want to check if it is callable or not.
- If the specified object is callable, callable() function returns True. Otherwise, it returns False.
2. Python callable() Function Examples.
2.1 Check whether a function or a string is callable.
- Here is an example of how to use python callable() function:
def my_function(): print("Hello, World!") print(callable(my_function)) # Output: True my_string = "Hello, World!" print(callable(my_string)) # Output: False
- When you run the above code, you will get the below output in console.
True False
- In the above example,
my_function()
is defined as a function andmy_string
is defined as a string. - When we pass
my_function
to thecallable()
function, it returnsTrue
becausemy_function
is a callable function. - However, when we pass
my_string
to thecallable()
function, it returnsFalse
becausemy_string
is not a callable function.
2.2 Check if a built-in function is callable.
- Below is the example code.
print(callable(print)) # Output: True
- In this example, we are checking if the built-in
print
function is callable or not. - Since
print
is a built-in function, it is callable and it will print True on the console.
2.3 Check if a class is callable.
- The below source code will demo how to check if a python class is callable or not.
class MyClass: def __init__(self): self.name = "John" def say_hello(self): print("Hello, " + self.name) obj = MyClass() print("MyClass is callable : " + str(callable(MyClass))) print("obj is callable : " + str(callable(obj))) print("obj.say_hello is callable : " + str(callable(obj.say_hello)))
- When you run the above example code, it will print out the below text on the screen.
MyClass is callable : True obj is callable : False obj.say_hello is callable : True
- In this example, we are defining a simple class
MyClass
with a constructor and a methodsay_hello()
. - We then create an object of the class and check if the class and the object are callable using the
callable()
function. - Since
MyClass
is a class definition, it is callable. - However, the object
obj
is not callable. - Finally, the method
say_hello()
is callable because it is a function defined within the class.
2.4 Check if a lambda function is callable.
- Below is the example source code.
my_lambda = lambda x: x**2 print(callable(my_lambda)) # Output: True
- In this example, we are defining a lambda function
my_lambda
that takes a single argumentx
and returns its square. - We then check if the lambda function is callable using the
callable()
function. - Since the lambda function is a function in Python, it is callable.
2.5 Check if a function defined in a module is callable.
- Below is the example source code.
import math print(callable(math.sqrt)) # Output: True
- In this example, we are importing the math module and checking if the
sqrt()
function defined in it is callable or not. Sincesqrt()
is a function, it is callable.