Programming Search⌘K

Featurecore

Callable objects and __call__

Also called callable, __call__, callable(), function object with state, call an instance, functor in Python, object is not callable.

A callable is an object that a call can call. Functions, methods and classes are callables, and an instance is a callable when its class defines __call__. The built-in callable() tests this.

Language
Python

Synopsis

class Counter:
    def __init__(self):
        self.calls = 0

    def __call__(self, text):
        self.calls += 1
        return text.upper()

shout = Counter()
print(shout("hi"), shout("there"))
print(shout.calls, callable(shout))
Output
HI THERE
2 True

shout is an instance, but the class defines __call__, so a program can call it like a function. It keeps its count in an attribute.

Semantics

r1. "A callable is an object that can be called, possibly with a set of arguments".2 "A function, and by extension a method, is a callable. An instance of a class that implements the call() method is also a callable."2

r2. __call__ is "Called when the instance is “called” as a function; if this method is defined, x(arg1, arg2, ...) roughly translates to type(x).call(x, arg1, ...)."1 Example: counter.

r3. "The object class itself does not provide this method."1 Example: not-callable.

r4. The primary of a call "must evaluate to a callable object (user-defined functions, built-in functions, methods of built-in objects, class objects, methods of class instances, and all objects having a call() method are callable)."4

r5. callable() returns "True if the object argument appears callable, False if not. If this returns True, it is still possible that a call fails, but if it is False, calling object will never succeed. Note that classes are callable (calling a class returns a new instance); instances are callable if their class has a call() method."3 Example: kinds.

Examples

class Plain:
    pass

class WithCall:
    def __call__(self):
        return "called"

for obj in [len, print, Plain, Plain(), WithCall(), "text".upper, 42]:
    print(type(obj).__name__, callable(obj))
Output
builtin_function_or_method True
builtin_function_or_method True
type True
Plain False
WithCall True
builtin_function_or_method True
int False

A class is callable, but an instance of Plain is not, because Plain has no __call__ (rule r5).

class Greeter:
    def __call__(self):
        return "class method"

g = Greeter()
g.__call__ = lambda: "instance attribute"
print(g())
Output
class method

A call uses type(x).__call__, so an attribute __call__ on the instance has no effect (rule r2).

Pitfalls

A call of an object whose type has no __call__ is a TypeError:

This code raises TypeError, on purpose.

count = 3
count()

A name that is bound to a value can hide a built-in function. After sum = 10, the call sum([1, 2]) raises the same TypeError, because sum is now an int.

Comparison

Go has no __call__ method. A Go method value can stand in for a callable object: "If the expression x has static type T and M is in the method set of type T, x.M is called a method value. The method value x.M is a function value that is callable with the same arguments as a method call of x.M."5 So a Go program can pass c.Count as a function, and the method keeps its receiver c. In Python, the object itself is the callable (rule r2).

References

  1. The Python Language Reference, Python 3.15, 3.3.6. Emulating callable objects
  2. Glossary, Python 3.15, callable
  3. Built-in Functions, Python 3.15 documentation, callable()
  4. The Python Language Reference, Python 3.15, 6.3.3. Calls
  5. The Go Programming Language Specification, Method values

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source