Python中的函数“/”参数

Alg*_*ra8 3 python collections

我注意到一些带有/参数的函数签名。可以在以下位置找到一个示例collections.Counter.__init__()

    def __init__(self, iterable=None, /, **kwds):
        '''Create a new, empty Counter object.  And if given, count elements
        from an input iterable.  Or, initialize the count from another mapping
        of elements to their counts.
        >>> c = Counter()                           # a new, empty counter
        >>> c = Counter('gallahad')                 # a new counter from an iterable
        >>> c = Counter({'a': 4, 'b': 2})           # a new counter from a mapping
        >>> c = Counter(a=4, b=2)                   # a new counter from keyword args
        '''
        super().__init__()
        self.update(iterable, **kwds)
Run Code Online (Sandbox Code Playgroud)

我一直无法找到它的用途,当我尝试在本地复制它时,我得到了一个SyntaxError.

任何有关它是什么以及为什么使用它的信息将不胜感激。

Avi*_*niv 5

的是在PEP570新语法描述:的使用“/”,以表明一些功能参数必须在位置上指定(即,不能用作为关键字参数)。
因此,将通过其位置传递的第一个参数与传递给字典的其余参数分开。
仅位置参数中阅读更多信息。