Fundamentals 4 min read

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.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
When Is an Object Callable in Python? A Deep Dive into callable()

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)
True

Extended 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)   # True

Classes 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) # True

Instances 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)   # True

Built‑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})    # False
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonprogrammingCallablebuilt-in functionobject
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.