仅限位置参数(/)
# 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
'''
标签:__,Python,demo2,demo1,参数,仅限
From: https://www.cnblogs.com/czzz/p/17410559.html