Zhi Jiang
Zhi Jiang

Reputation: 23

Unknown Python syntax in PyTorch: a instance can directly receive a parameter

I have a question about Python syntax when I'm learning PyTorch. The following codes are an example from the PyTorch document.

m = nn.Linear(20, 30)
input = autograd.Variable(torch.randn(128, 20))
output = m(input)
print(output.size())

This first line is to create an instance m, but why this instance m can directly receive a parameter like the line 3? I think it should use method to deal with parameter like m.method(input).

Upvotes: 2

Views: 55

Answers (1)

excalibur1491
excalibur1491

Reputation: 1221

In python, any object can define a __call__ method that allows it to be used as a function like in your example.

Reference: https://docs.python.org/2/reference/datamodel.html#object.call

Upvotes: 2

Related Questions