Reputation: 1
I'm trying to understand someone else's code in Python and I stumbled across a line I don't quite understand and which I can't find on the internet:
x=self(k)
with k being a torch-array. I know what self.something does but I haven't seen self(something) before.
Upvotes: 0
Views: 73
Reputation: 70267
self
, for these purposes, is just a variable like any other, and when we call a variable with parentheses, it invokes the __call__
magic method. So
x = self(k)
is effectively a shortcut for
x = self.__call__(k)
Footnote: I say "effectively", because it's really more like
x = type(self).__call__(self, k)
due to the way magic methods work. This difference shouldn't matter unless you're doing funny things with singleton objects, though.
Upvotes: 3