\* 在 PyTorch 的函数签名中是什么意思?

What does \* mean in the function signature of PyTorch?

例如在 randint signature 中有一个 \* 作为第四个参数。这是什么意思?

torch.randint(low=0, high, size, \*, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

我知道 Python 3.8 中引入的仅位置参数和仅关键字参数,它们使用 \*。但在这里我看到 \*.

我很确定 user2357112 支持 Monica 是正确的。为了确认这一点,我尝试寻找文档的来源,which is autogenerated. I've looked at the implementation, which is in C++ where functions signatures work differently. I found the type annotation generation code:

        'randint': ['def randint(low: _int, high: _int, size: _size, *,'
                    ' generator: Optional[Generator]=None, {}) -> Tensor: ...'
                    .format(FACTORY_PARAMS),
                    'def randint(high: _int, size: _size, *,'
                    ' generator: Optional[Generator]=None, {}) -> Tensor: ...'
                    .format(FACTORY_PARAMS)],

这表明评论者确实是对的,应该是*而不是\*

Python 中的位置参数由 / 表示,而不是 \。在 Python 的语法中,\ 仅在两个地方使用:作为字符串文字中的转义字符,以及在行尾作为明确的行继续标记。


看起来 this has been an issue 至少从 2020 年 5 月开始,之后没有 activity。