I was investigating the PyTorch codebase, and came across an interesting quirk regarding how they implemented the __call__ method for the Module class, which is in torch.nn. It is an important class as it is the base class for all neural network modules.
Two points of clarification:
i) I understand what __call__ does in general. It allows you to call an instance of a class as if it were a function. The question is about the manner in which the method is defined in the example below.
ii) although I make reference to an example from PyTorch, I am asking this as a general question about implementing classes in Python
The skeleton of the class is as follows
class Module:
...
def _call_impl(self, *input, **kwargs):
...
__call__ = _call_impl
My question is, why assign the __call__ method in this way? Why not do this
class Module:
...
def __call__(self, *input, **kwargs):
# all the code originally in _call_impl
...
Is there a difference between these two ways of creating the __call__ method? If so, what is that difference?
(_call_impl is defined here, and the assignment to __call__ happens just afterwards, at this line)