callable()
Reflection check for the "can I put ()" question — but not a guarantee the call will succeed.
Common call
if callable(handler):
Returns
True or False
Replaces
`hasattr(x, "__call__")` — but the builtin is faster and clearer
Watch out
Classes are ALWAYS callable (their constructor); instances only if they define __call__
callable(objectobject — Any object. callable checks whether the type has a __call__ method — a surface-level check that does not actually try to invoke.type: Any · required)
→ bool
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| object | Any | yes | Any object. callable checks whether the type has a __call__ method — a surface-level check that does not actually try to invoke. |
Return value
bool — True if the object appears callable (has __call__ in its type). May return True for objects whose __call__ would raise when actually invoked — it is a surface-level check, not a guarantee that the call will succeed.
Common patterns
Guard before invoking
Defensive check when a callback might be missing or malformed.
if callable(callback): callback(event)
Dispatch table validation
Reject non-callable entries in a lookup table.
handlers = {name: h for name, h in registry.items() if callable(h)}
Check for a decorator target
Decorators should verify they got a callable.
def logged(func): if not callable(func): raise TypeError("must decorate a callable") ...
Duck-type: has __call__ vs is callable
callable is faster and more correct than the hasattr equivalent.
# WORSE: hasattr(x, "__call__") # BETTER: callable(x)
Examples
1. Class
callable(str)
Returns
True2. Function
callable(len)
Returns
True3. Lambda
callable(lambda x: x)
Returns
True4. Built-in
callable(print)
Returns
True5. Integer value
callable(42)
Returns
False6. String value
callable("hi")
Returns
False7. None
callable(None)
Returns
False8. Method
callable("hi".upper)
Returns
True9. Custom __call__
class C: __call__=lambda self: 1
callable(C())
Returns
TruePitfalls
1. callable(x) is True does NOT guarantee x() will succeed
The check is surface-level — it says "the type has a __call__". The actual call may still raise TypeError (wrong arity), NotImplementedError, or anything else the callable chooses.
Assumed safety
if callable(func): func() # may still raise
TypeError: ... missing required arg
Try/except
try: result = func() except TypeError as e: ...
catches wrong-arity
2. Classes are ALWAYS callable — calling constructs an instance
A class is a callable that returns a new instance. This surprises people who assume "callable" means "function".
Assumed False
callable(list)
True # list() returns a new empty list
Read as "constructor"
x = list() # calling the class constructs
[]
3. Instances of user classes ARE callable if the class defines __call__
You can make an instance behave like a function by defining __call__. This is a common Python pattern for stateful callables — used by functools, decorators, dispatchers.
Assumed uncallable
class Counter: def __call__(self): return 1 callable(Counter())
True
That is the design
c = Counter() c() # 1
stateful callable
4. callable was removed in Python 3.0, then reinstated in 3.2
A footgun for anyone maintaining code that ran on Python 3.0 or 3.1 — the builtin was briefly removed. If your project supports those, use `hasattr(x, "__call__")`. Otherwise callable is fine.
3.0-3.1 issue
callable(x) # NameError on 3.0/3.1
Portable
hasattr(x, "__call__")
works everywhere
When to use
Use it
- Guarding before invoking a possibly-missing callback
- Filtering a registry to callable entries
- Validating decorator arguments
- Reflection code that dispatches based on "is this a function?"
Reach for something else
- You know it is callable → skip the check, catch TypeError if needed
- Type-based check → isinstance is more specific
- Surface guarantee — for actual correctness, try to call and catch errors
- Checking function signature → inspect.signature is the right tool
Notes
Complexity
O(1) — checks the type slot
Return
bool — True or False
CPython impl
Python/bltinmodule.c :: builtin_callable — checks tp_call
Memory
No allocation
Thread-safe
Yes
FAQ
Because calling a class INVOKES its constructor: `MyClass(...)` returns a new instance. Under the hood the class type has a __call__ method that runs __new__ + __init__. So classes are the ubiquitous case of "callable" that people forget.
History
1.0
callable() has been a builtin since Python 1.0.
3.0
REMOVED in a "cleanup" that was widely disliked.
3.2
REINSTATED — the removal was reversed by popular demand.