What is `__call__` in Python and what does implementing it on a class enable?

Python Developer Hard

Python Developer — Hard

What is `__call__` in Python and what does implementing it on a class enable?

Key points

  • `__call__` enables instances of a class to be called like functions
  • It is used for creating callable objects and class-based decorators
  • The method is executed when the instance is called with ()
  • Objects implementing __call__ can retain state between calls

Ready to go further?

Related questions