Python 函数:https://www.cnblogs.com/poloyy/p/15092393.html
def test1(a, b, c): print(a, b, c) test1(a=1, b=2, c=3) def test(a, /, b, c): print(a, b, c) # 正确 test(1, b=2, c=3) test(*(1,), b=2, c=3) # 错误 test(a=1, b=2, c=3) 1 2 3 1 2 3 1 2 3 test(a=1, b=2, c=3) TypeError: test() got some positional-only arguments passed as keyword arguments: ‘a‘
def f1(a, *, b, c): return a + b + c # 正确 f1(1, b=2, c=3) f1(1, **{"b": 2, "c": 3}) # 错误 f1(1, 2, c=3) # 输出结果 6 6 f1(1, 2, c=3) TypeError: f1() takes 1 positional argument but 2 positional arguments (and 1 keyword-only argument) were given
def f(a, /, b, *, c): print(a, b, c) # 正确 f(1, 2, c=3) f(1, b=2, c=3) # 错误 f(a=1, b=2, c=3) f(1, 2, 3) # 输出结果 1 2 3 1 2 3
def f(a, b, /, c, *, d, e): print(a, b, c, d, e) # 正确 f(1, 2, c=3, d=4, e=5) # 错误 f(1, 2, 3, 4, 5) # 输出结果 1 2 3 4 5
Python - 3.8 新特性之仅位置参数 & 仅关键字参数
原文:https://www.cnblogs.com/poloyy/p/15106522.html