'Variable args and keyword only args in a function
I was going thru PyTorch and found function signatures like
torch.zeros(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
Here the args after * are keyword only args and the size is a variable number of args.
My questions are
- When I try to define a function in similar lines
def function(*a, *, e=0, f=0):
print (a, e, f)
function(1, e = 5, f = 6)
I get the following error
File "main.py", line 2
def function(*a, *, e=0, f=0):
^
SyntaxError: invalid syntax
- Also
torch.zeros
acceptssize
as keyword. How is this possible ?
torch.zeros(size=(2,3))
AFAIK only these 2 should work
torch.zeros((2,3))
torch.zeros(2,3)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|