“ /”在split(self,/,sep = None,maxsplit = -1)中表示什么?

问题描述 投票:1回答:1
str.split = split(self, /, sep=None, maxsplit=-1)
    Return a list of the words in the string, using sep as the delimiter string.

    sep
      The delimiter according which to split the string.
      None (the default value) means split according to any whitespace,
      and discard empty strings from the result.
    maxsplit
      Maximum number of splits to do.
      -1 (the default value) means no limit.

/,似乎是第二个参数,对我来说是个新符号。那里在做什么?

python-3.x syntax
1个回答
1
投票

来自What's New in Python 3.8

仅位置参数

[有一个新的函数参数语法/,用于指示某些函数参数必须在位置上指定,并且不能用作关键字参数。

在以下示例中,参数ab仅是位置,而cd可以是位置或关键字,而ef必须是关键字:

def f(a, b, /, c, d, *, e, f):
    print(a, b, c, d, e, f)

此符号的一个用例是,它允许纯Python函数完全模拟现有C编码函数的行为。

© www.soinside.com 2019 - 2024. All rights reserved.