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.
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.
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:
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:
- Python creates the instance
__init__initializes it
Invocation is a separate later step:
- the instance already exists
- 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:
__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.

