What is the difference between call() and __call__() method in python?

Question:

In the tensorflow documentation I see the call() method defined when subclassing activations and models. However, in case of subclassing regularizers, initializers and constraints, they define the __class__() method instead.

When playing around with both, I could not find any differences myself.

Could someone tell me what the difference is?

Asked By: tijmenvanetten

||

Answers:

__call__ is a python magic method (or dunder method) that makes class objects callable. but on the other hand call is a user-defined method in Keras which in the background uses mentioned __call__ method but before using it this user-defined call does some extra things like building weight and bias tensors based on the input tensor shape.

Answered By: d4riush

call() is just a regular method that you can call on an instance of a class, e.g. foo.call(...).

__call__() is a special method that makes the instance itself callable. So instead of doing foo.call(...) you can just do foo(...). (You can also do foo.__call__() still.)

Answered By: kindall
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.