Python
init method
call method
programming
Python functions

What is the difference between __init__ and __call__?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

__init__ and __call__ are both special methods in Python, but they happen at completely different moments in an object's life. __init__ runs when the object is being created, while __call__ runs later if you use the object itself like a function.

A simple way to remember the difference is this: __init__ prepares the object, and __call__ defines what the object does when invoked.

What __init__ Does

__init__ is the initializer. Python calls it after a new instance has been created.

python
1class Greeter:
2    def __init__(self, name):
3        self.name = name
4
5
6obj = Greeter("Alice")
7print(obj.name)

Here, __init__ stores the initial state. It is not something you usually call manually. Its job is to set up the instance so the object is ready to use.

Important points about __init__:

  • it runs during construction
  • it typically saves arguments onto self
  • it returns None
  • it is about initialization, not function-like behavior

What __call__ Does

__call__ makes an instance callable. If a class defines __call__, then an instance of that class can be used with parentheses just like a function.

python
1class Greeter:
2    def __init__(self, name):
3        self.name = name
4
5    def __call__(self, punctuation="!"):
6        return f"Hello, {self.name}{punctuation}"
7
8
9obj = Greeter("Alice")
10print(obj())
11print(obj("."))

The object is created once, but __call__ can run many times afterward.

That is the core difference:

  • 'Greeter("Alice") triggers object construction and therefore __init__'
  • 'obj() triggers __call__'

Why __call__ Is Useful

__call__ is useful when an object should behave like a stateful function. The object can keep configuration or history in its attributes while still being invoked with function syntax.

Example:

python
1class Counter:
2    def __init__(self):
3        self.count = 0
4
5    def __call__(self):
6        self.count += 1
7        return self.count
8
9
10counter = Counter()
11print(counter())
12print(counter())
13print(counter.count)

A normal function could count too if it closed over state, but a callable object makes that state explicit and easier to extend.

This pattern appears in callbacks, decorators, small command objects, and machine-learning code where an object behaves like an operation while still carrying internal state.

Construction Versus Invocation

The easiest mistake is to think that __call__ is some kind of alternative constructor. It is not.

Construction normally involves:

  1. Python creates the instance
  2. __init__ initializes it

Invocation is a separate later step:

  1. the instance already exists
  2. calling instance(...) runs __call__

So if you already have an object and put parentheses after it, Python is not creating a new instance. It is invoking the existing instance.

A Combined Example

This example shows the contrast clearly:

python
1class Multiplier:
2    def __init__(self, factor):
3        print("running __init__")
4        self.factor = factor
5
6    def __call__(self, value):
7        print("running __call__")
8        return value * self.factor
9
10
11m = Multiplier(3)
12print(m(10))
13print(m(20))

__init__ runs once when m is created. __call__ runs each time m(...) is used.

Common Pitfalls

The most common mistake is expecting __init__ to return a value. It should initialize the object and return None.

Another mistake is confusing class calls with instance calls. Calling the class, such as Greeter("Alice"), creates an object. Calling the instance, such as obj(), uses __call__ if it exists.

A third issue is adding __call__ when a regular named method would be clearer. Callable objects are powerful, but they should make the API more natural, not more mysterious.

Summary

  • '__init__ initializes a newly created object.'
  • '__call__ runs when an existing object is used like a function.'
  • '__init__ usually stores state; __call__ usually performs an action using that state.'
  • '__init__ runs at construction time, while __call__ can run many times later.'
  • Use __call__ when a stateful object should behave naturally like a function.

Course illustration
Course illustration

All Rights Reserved.