tijmenvanetten
tijmenvanetten

Reputation: 111

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

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?

Upvotes: 3

Views: 1513

Answers (2)

d4riush
d4riush

Reputation: 430

__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.

Upvotes: 6

kindall
kindall

Reputation: 184151

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.)

Upvotes: 4

Related Questions