我在看glob函数的定义,我注意到第二个参数很简单*.
def glob(pathname, *, recursive=False):
"""Return a list of paths matching a pathname pattern.
[...]
"""
return list(iglob(pathname, recursive=recursive))
Run Code Online (Sandbox Code Playgroud)
有什么意义*?
Pik*_*les 10
后面的所有参数都*必须明确指定其名称。例如,如果你有这个函数:
def somefunction(a,*,b):
pass
Run Code Online (Sandbox Code Playgroud)
你可以这样写:
somefunction(0, b=0)
Run Code Online (Sandbox Code Playgroud)
但不是这个:
somefunction(0, 0)
Run Code Online (Sandbox Code Playgroud)
该*指示的位置参数的结尾.之后的每个参数只能通过关键字指定.这在PEP 3102中定义
>>> def foo1(a, b=None):
... print(a, b)
...
>>> def foo2(a, *, b=None):
... print(a, b)
...
>>> foo1(1, 2)
1 2
>>> foo2(1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo1() takes 1 positional argument but 2 were given
>>> foo2(1, b=2)
1 2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1043 次 |
| 最近记录: |