Reputation: 23
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
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