Table of Contents
The callable() method returns True if the object passed appears callable. If not, it returns False.
The syntax of callable() is:
callable(object)
1. callable() Parameters
callable() method takes a single argument object.
2. Return value from callable()
callable() method returns:
True– if the object appears callableFalse– if the object is not callable.
It important to remember that, even if callable() is True, call to the object may still fail.
However, if callable() returns False, call to the object will certainly fail.
3. Example 1: How callable() works?
x = 5
print(callable(x))
def testFunction():
print("Test")
y = testFunction
print(callable(y))
Output
False True
Here, the object x is not callable. And, the object y appears to be callable (but may not be callable).
4. Example 2: Callable Object
class Foo:
def __call__(self):
print('Print Something')
print(callable(Foo))
Output
True
The instance of Foo class appears to be callable (and is callable in this case).
class Foo:
def __call__(self):
print('Print Something')
InstanceOfFoo = Foo()
# Prints 'Print Something'
InstanceOfFoo()
5. Example 3: Object Appears to be Callable but isn’t callable.
class Foo:
def printLine(self):
print('Print Something')
print(callable(Foo))
Output
True
The instance of Foo class appears to be callable but it’s not callable. The following code will raise an error.
class Foo:
def printLine(self):
print('Print Something')
print(callable(Foo))
InstanceOfFoo = Foo()
# Raises an Error
# 'Foo' object is not callable
InstanceOfFoo()
Output
True Traceback (most recent call last): File "", line 10, in TypeError: 'Foo' object is not callable
Related posts:
Python Modules
Python Program to Find Numbers Divisible by Another Number
Python String isidentifier()
Python Strings
Python Program to Add Two Matrices
Python Dictionary popitem()
Python Dictionary clear()
Python map()
Python Keywords and Identifiers
Python String lower()
Python List append()
Python reversed()
Python Program to Print Colored Text to the Terminal
Python sum()
Python abs()
Python Program to Access Index of a List Using for Loop
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Program to Find ASCII Value of Character
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python String swapcase()
Python Inheritance
Python Object Oriented Programming
Python issubclass()
Python Program to Compute all the Permutation of the String
Python Program to Find Hash of File
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python any()
Python memoryview()
Python Program to Create a Countdown Timer
Python set()
Python staticmethod()
Python Program to Check if a Number is Odd or Even