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 locals()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python zip()
Python setattr()
Python all()
Python isinstance()
Python strptime()
Python Program to Find All File with .txt Extension Present Inside a Directory
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python issubclass()
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python Program to Return Multiple Values From a Function
Python String lstrip()
Python int()
Python String upper()
Python Program to Convert String to Datetime
Python Get Current time
Python ord()
Python Program to Illustrate Different Set Operations
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python String isspace()
Python Program to Sort a Dictionary by Value
Python Program to Calculate the Area of a Triangle
Python String replace()
Python String islower()
Python Set isdisjoint()
Python Dictionary popitem()
Python List count()
Python String isdecimal()
Python String rsplit()
Python Dictionary get()