Python - 仅限关键字参数 & 仅限位置参数
仅限位置参数(/)
# Python 3.8 新增
def demo1(a,b,/):
return a,b
if __name__ == '__main__':
demo1(1,2) # right
demo1(1,b=2) # error
# demo1(1,b=2) 报错:
'''
Traceback (most recent call last):
File "E:\PyProject\pytestDemo\demo10.py", line 19, in <module>
demo1(1,b=2) # error
TypeError: demo1() got some positional-only arguments passed as keyword arguments: 'b'
'''
仅限关键字参数(*)
# Python3 新增
def demo2(a,*,b,c):
return a,b,c
if __name__ == '__main__':
demo2(1,b=2,c=3) # right
demo2(1,2,3) # error
# demo2(1,2,3) 报错:
'''
Traceback (most recent call last):
File "E:\PyProject\pytestDemo\demo10.py", line 18, in <module>
demo2(1,2,3)
TypeError: demo2() takes 1 positional argument but 3 were given
'''
本文来自博客园,作者:chuangzhou,转载请注明原文链接:https://www.cnblogs.com/czzz/p/17410559.html