我注意到一些帶有/參數的函數簽名??梢栽谝韵挛恢谜业竭@方面的示例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)我無法找到它的用途,當我嘗試在本地復制它時,我得到一個SyntaxError.任何關于它是什么以及為什么使用它的信息將不勝感激。
2 回答

慕田峪7331174
TA貢獻1828條經驗 獲得超13個贊
PEP570 中描述的新語法:使用“/”表示某些函數參數必須按位置指定(即,不能用作關鍵字參數)。
因此,將通過其位置傳遞的第一個參數與傳遞給字典的其余參數分開。

陪伴而非守候
TA貢獻1757條經驗 獲得超8個贊
它是 Python 3.8 中的新功能。/ 之前的所有參數都是位置參數,不能使用關鍵字指定。
在上面給出的示例中,寫 . 不再合法Counter(iterable=(1,2,3))
。
添加回答
舉報
0/150
提交
取消