__call__

def __call__(self, ...)
Returns: any · Updated March 18, 2026 · Dunder Methods
python oop dunder-methods

__call__

The __call__ method lets you treat an instance of a class like a function. Instead of calling a method on an object, you call the object itself—and Python invokes __call__ behind the scenes.

Overview

When you define __call__(self, ...) in a class, instances of that class become callable. That means you can write instance() instead of instance.method(). It’s a clean way to create objects that behave like functions while maintaining state.

Python invokes __call__ automatically whenever you treat an object as a callable:

obj()        # Calls obj.__call__()
obj(arg1, arg2)  # Calls obj.__call__(arg1, arg2)

Syntax

def __call__(self, *args, **kwargs):
    # Your logic here
    return something

Key points:

  • Takes self plus any arguments you want to pass
  • Can return any type—not just strings like __str__
  • Arguments pass through exactly as if you were calling a function

How It Works

Every object in Python has a __call__ method—or inherits one. By default, the inherited version raises TypeError: '...' object is not callable. When you define your own, you replace that behavior.

class Greeter:
    def __call__(self, name):
        return f"Hello, {name}!"

greet = Greeter()
print(greet("Alice"))  # Hello, Alice!
print(greet("Bob"))    # Hello, Bob!

The instance greet is now callable. It remembers it’s an instance of Greeter, but works like a function.

Basic Example

Here’s a simple counter that tracks how many times it’s been called:

class Counter:
    def __init__(self):
        self.count = 0
    
    def __call__(self):
        self.count += 1
        return self.count

counter = Counter()
print(counter())  # 1
print(counter())  # 2
print(counter())  # 3

Each call to counter() increments and returns the count. The state persists between calls—something a plain function can’t do without closures or global variables.

Common Use Cases

Callable Classes as Decorators

One of the most practical uses for __call__ is building decorators. A decorator is any callable that takes a function and returns a new function. Classes with __call__ fit this pattern perfectly:

class Timer:
    def __init__(self, func):
        self.func = func
    
    def __call__(self, *args, **kwargs):
        import time
        start = time.time()
        result = self.func(*args, **kwargs)
        end = time.time()
        print(f"{self.func.__name__} took {end - start:.4f} seconds")
        return result

@Timer
def slow_function():
    import time
    time.sleep(0.1)
    return "done"

slow_function()  # slow_function took 0.1010 seconds

Function-Like Objects

Use __call__ when you need an object that carries state but behaves like a function. For example, an adder that remembers its base value:

class Adder:
    def __init__(self, base):
        self.base = base
    
    def __call__(self, value):
        return self.base + value

add_five = Adder(5)
add_ten = Adder(10)

print(add_five(3))   # 8
print(add_five(10))  # 15
print(add_ten(3))    # 13

This is cleaner than using closures in many cases, especially when you need to inspect or serialize the object later.

Stateful Callbacks

Event systems and frameworks often use callable objects for callbacks. The object can hold configuration or state while remaining callable:

class ClickHandler:
    def __init__(self, action):
        self.action = action
        self.clicks = 0
    
    def __call__(self):
        self.clicks += 1
        print(f"Click #{self.clicks}: {self.action}")

on_submit = ClickHandler("Submit form")
on_submit()  # Click #1: Submit form
on_submit()  # Click #2: Submit form

Factory Functions

Classes with __call__ can act as factories that create configured functions on the fly:

class Multiplier:
    def __init__(self, factor):
        self.factor = factor
    
    def __call__(self, value):
        return value * self.factor

double = Multiplier(2)
triple = Multiplier(3)

numbers = [1, 2, 3]
print([double(x) for x in numbers])  # [2, 4, 6]
print([triple(x) for x in numbers])  # [3, 6, 9]

__call__ vs Regular Methods

The difference is in how you invoke them:

class Calculator:
    def add(self, a, b):
        return a + b
    
    def __call__(self, a, b):
        return a + b

calc = Calculator()

# Regular method—two steps
result = calc.add(2, 3)  # 5

# Callable instance—one step
result = calc(2, 3)      # 5

Both do the same thing. The callable version is syntactic sugar that can make your API cleaner when the object fundamentally represents an action or operation.

Summary

  • __call__ makes an instance callable like a function
  • Invoked automatically when you write instance(args)
  • Useful for decorators, stateful callbacks, function factories, and any object that represents an operation
  • Can accept any arguments and return any value
  • Lets you combine the expressiveness of objects (state, methods) with the syntax of functions

Define __call__ when you want objects that work like functions but need to maintain internal state.

See Also

  • __init__ — Constructor method called on instance creation
  • __str__ — String representation of an instance
  • __repr__ — Developer-friendly representation for debugging