Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is using __call__ a good idea?

Tags:

python

oop

What are peoples' opinions on using the __call__. I've only very rarely seen it used, but I think it's a very handy tool to use when you know that a class is going to be used for some default behaviour.

like image 837
Tim McNamara Avatar asked Jul 30 '10 08:07

Tim McNamara


People also ask

What is the use of __ call __ in Python?

The __call__ method enables Python programmers to write classes where the instances behave like functions and can be called like a function. When the instance is called as a function; if this method is defined, x(arg1, arg2, ...) is a shorthand for x. __call__(arg1, arg2, ...) .

What is the purpose of the magic method __ init __?

__init__ : "__init__" is a reseved method in python classes. It is known as a constructor in object oriented concepts. This method called when an object is created from the class and it allow the class to initialize the attributes of a class.

What is __ method in Python?

Instead of writing another method to perform certain operations, we can use the __call__ method to directly call from the instance name. The call_example instance can be directly called as a method since we have defined the __call__ method in the class.

What does __ class __ mean in Python?

__class__ is an attribute on the object that refers to the class from which the object was created. a. __class__ # Output: <class 'int'> b. __class__ # Output: <class 'float'> After simple data types, let's now understand the type function and __class__ attribute with the help of a user-defined class, Human .


1 Answers

I think your intuition is about right.

Historically, callable objects (or what I've sometimes heard called "functors") have been used in the OO world to simulate closures. In C++ they're frequently indispensable.

However, __call__ has quite a bit of competition in the Python world:

  • A regular named method, whose behavior can sometimes be a lot more easily deduced from the name. Can convert to a bound method, which can be called like a function.
  • A closure, obtained by returning a function that's defined in a nested block.
  • A lambda, which is a limited but quick way of making a closure.
  • Generators and coroutines, whose bodies hold accumulated state much like a functor can.

I'd say the time to use __call__ is when you're not better served by one of the options above. Check the following criteria, perhaps:

  • Your object has state.
  • There is a clear "primary" behavior for your class that's kind of silly to name. E.g. if you find yourself writing run() or doStuff() or go() or the ever-popular and ever-redundant doRun(), you may have a candidate.
  • Your object has state that exceeds what would be expected of a generator function.
  • Your object wraps, emulates, or abstracts the concept of a function.
  • Your object has other auxilliary methods that conceptually belong with your primary behavior.

One example I like is UI command objects. Designed so that their primary task is to execute the comnand, but with extra methods to control their display as a menu item, for example, this seems to me to be the sort of thing you'd still want a callable object for.

like image 191
Owen S. Avatar answered Sep 23 '22 14:09

Owen S.