When Is an Object Callable in Python? A Deep Dive into callable()
The article explains Python's built‑in callable() function, detailing how it determines whether objects such as functions, methods, classes, lambdas, and instances with a __call__ method are callable, and provides numerous code examples illustrating true and false cases for various object types.
What is callable()
Python's built‑in callable() function checks whether an object can be called like a function. It returns True for functions, methods, lambdas, classes, and instances that define a __call__ method; otherwise it returns False. A True result does not guarantee that the call will succeed, but a False result guarantees it will fail.
Syntax
callable(object)Parameters and Return Value
object – the object to test.
Returns True if the object is callable, otherwise False.
Basic Examples
> callable(0)
False
> callable("runoob")
False
> def add(a, b):
... return a + b
> callable(add)
True
> class A:
... def method(self):
... return 0
> callable(A)
True
> a = A()
> callable(a)
False
> class B:
... def __call__(self):
... return 0
> callable(B)
True
> b = B()
> callable(b)
TrueExtended Examples
Using help(callable) shows the official documentation.
print(help(callable))
# callable(object) -> bool
# Return whether the object is callable (i.e., some kind of function).
# Note that classes are callable, as are instances with a __call__() method.Additional demonstrations:
Functions and lambdas are callable:
f = lambda x, y: x + y
f(2, 3) # 5
callable(f) # TrueClasses are callable, and their methods are callable:
class C:
def printf(self):
print('This is class C!')
callable(C) # True
callable(C.printf) # True
objC = C()
callable(objC.printf) # TrueInstances are callable only if they implement __call__:
class A:
def printf(self):
print('This is class A!')
objA = A()
callable(A) # True
callable(objA) # False
class B:
def __call__(self):
print('This is class B!')
objB = B()
callable(B) # True
callable(objB) # TrueBuilt‑in types such as integers, strings, lists, tuples, and dictionaries are not callable:
callable(2) # False
callable('python') # False
callable([1,2,3]) # False
callable((4,5,6)) # False
callable({'a':1}) # FalseSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
