In this article, we will see how to check if an object is callable in Python. In general, a callable is something that can be called. This built-in method in Python checks and returns True if the object passed appears to be callable, but may not be, otherwise False.
Python callable()
In Python, callable() function is a built-in function that we can use to check if an object is callable in Python i.e., it can be called like a function. It generally returns True if the object can be called and False if not.
callable() in Python Syntax
Syntax: callable(object)
The callable() method takes only one argument, an object, and returns one of the two values
- returns True, if the object appears to be callable.
- returns False, if the object is not callable.
How callable() work in Python?
In this example, we have used callable to find out if the object is callable or not.
Python3
x = 10
print(callable(x))
def geeks(x):
return (x)
y = geeks
print(callable(y))
Different Scenario for Python callable()
There may be few cases where callable() works differently and returns True but the call to object fails. But if a case returns False, the calling object will never succeed.
When Object is Callable
In this example, we are using callable() function to check if an object is callable in Python. In the first case when an object is passed in the callable() method, it returns True. It is so because let is an object to the callable function Geek (which may not be in all cases). In the second case num is absolutely not a callable object, so the result is False.
Python3
# Python program to illustrate
# callable() a test function
def Geek():
return 5
# an object is created of Geek()
let = Geek
print(callable(let))
# a test variable
num = 5 * 5
print(callable(num))
Output
True
False
In this example, we have made a class Geek to check if the class and object is callable or not.
Python3
# Python program to illustrate callable()
class Geek:
def __call__(self):
print('Hello GeeksforGeeks')
# Suggests that the Geek class is callable
print(callable(Geek))
# This proves that class is callable
GeekObject = Geek()
GeekObject()
Output
True
Hello GeeksforGeeks
When Object is NOT callable
In this example, we will using callable() function to check if the object is callable or not in Python. Here the object is not callable. The callable() method returns True suggesting that the Geek class is callable, but the instance of Geek is not callable() and it returns a runtime error.
Python3
# Python program to illustrate callable()
class Geek:
def testFunc(self):
print('Hello GeeksforGeeks')
# Suggests that the Geek class is callable
print(callable(Geek))
GeekObject = Geek()
# The object will be created but
# returns an error on calling
GeekObject()
Output:
True
Traceback (most recent call last):
File "/home/3979dc83032f2d29befe45b6ee6001a4.py", line 10, in
GeekObject()
TypeError: 'Geek' object is not callable
Similar Reads
__call__ in Python Python has a set of built-in methods and __call__ is one of them. The __call__ method enables Python programmers to write classes where the instances behave like functions and can be called like a function. When this method is defined, calling an object (obj(arg1, arg2)) automatically triggers obj._
4 min read
eval in Python Python eval() function parse the expression argument and evaluate it as a Python expression and runs Python expression (code) within the program.Python eval() Function SyntaxSyntax: eval(expression, globals=None, locals=None)Parameters:expression: String is parsed and evaluated as a Python expressio
5 min read
bool() in Python In Python, bool() is a built-in function that is used to convert a value to a Boolean (i.e., True or False). The Boolean data type represents truth values and is a fundamental concept in programming, often used in conditional statements, loops and logical operations.bool() function evaluates the tru
3 min read
float() in Python Python float() function is used to return a floating-point number from a number or a string representation of a numeric value. Example: Here is a simple example of the Python float() function which takes an integer as the parameter and returns its float value. Python3 # convert integer value to floa
3 min read
classmethod() in Python The classmethod() is an inbuilt function in Python, which returns a class method for a given function. This means that classmethod() is a built-in Python function that transforms a regular method into a class method. When a method is defined using the @classmethod decorator (which internally calls c
8 min read
call() decorator in Python Python Decorators are important features of the language that allow a programmer to modify the behavior of a class. These features are added functionally to the existing code. This is a type of metaprogramming when the program is modified at compile time. The decorators can be used to inject modifie
3 min read
bin() in Python Python bin() function returns the binary string of a given integer. bin() function is used to convert integer to binary string. In this article, we will learn more about Python bin() function. Example In this example, we are using the bin() function to convert integer to binary string. Python3 x = b
2 min read
exec() in Python exec() function is used for the dynamic execution of Python programs which can either be a string or object code. If it is a string, the string is parsed as a suite of Python statements which is then executed unless a syntax error occurs and if it is an object code, it is simply executed. We must be
4 min read
Python lambda In Python, an anonymous function means that a function is without a name. As we already know that def keyword is used to define the normal functions and the lambda keyword is used to create anonymous functions.Python lambda Syntax:lambda arguments : expressionPython lambda Example:Pythoncalc = lambd
4 min read
Python compile() Function Python is a high-level, general-purpose, and very popular programming language. In this article, we will learn about the Python compile() function. Python compile() Function SyntaxPython compile() function takes source code as input and returns a code object that is ready to be executed and which ca
3 min read