Python callable() function
Python built-in function callable()
is used to check or verify whether a given object or entity can be called or not.
It takes an entity is an argument that may be of any type such as number, string, object or class, etc, and returns a boolean value.
If it returns true
then the object can be called inside the program and false
means you cannot call that particular object.
Syntax
callable(parameter)
Table of Contents
Examples
x=15 print(callable(x)) # returns False
Variable x
is a data-type which is not callable hence callable()
function returns False
.
Using callable() on a function
def show_name(): print("Pavan") print(callable(show_name)) # returns True
Functions are callable hence it returns True
.
def show_age(age): print(age) print(callable(show_age)) # returns True
callable()
function.Using callable() on Classes and Object methods
class Person: def __init__(self, name, age): print("My name is {} and i'm {} years old".format(name, age)) print(callable(Person)) #returns True
callable() function will also return True
for class and object methods.But not on objects explained below example.
class Person: def __init__(self): pass def show_name(self): print("My Name is Kumar") p1 = Person() print(callable(p1)) # returns False print(callable(p1.show_name)) # returns True print(callable(p1.invalid_method_name)) # Throws AttributeError : Person' object has no attribute 'invalid_method_name'
In the above example object p1
of class Person
returns False
on callable(p1)
. But when passed with the object show_name
method it returns True
which means that the object may not be callable but the object method is callable.
If the wrong method is passed with an object were that method doesn’t exist then AttributeError : Person' object has no attribute 'invalid_method_name'
is thrown.
To be noted that class may contain methods which may belong to classmethod
, staticmethod
and object
method. So when the object method is called from class this will lead to an exception.
Key Takeaways
- callable() function validates whether the given object is callable or not callable.
- This function doesn’t work with data-types such as string, number, etc, and returns False.
- Some cases were callable will returns True when called in class.
- callable returns False when called on the object. But when called on object followed by the method will return True,
- Returns
NameError
for the invalid function name. - Returns
AttributeError : object has no attribute
for the invalid object method name.
Conclusion
We have come to the end of our post on the Python callable() function. Motivate us by sharing this post and if you have any doubts changes to be made than comment below. We’ll address your queries shortly.
Related Posts
- Demystifying Python Calendar Module with Examples for beginners
- What are Positional-only Parameters and how to use them in Python 3.8?




