最近 PEP 570被接受了,其实要加的这个Positional-Only Parameters
原来在内置的C函数上有很多都用到了:
- In : __builtin__.eval
- Out: <function eval(source, globals=None, locals=None, /)>
- In : __builtin__.len
- Out: <function len(obj, /)>
- In : __builtin__.divmod
- Out: <function divmod(x, y, /)>
看它们的签名,最后都有一个/,/用途是 在/左面的这些参数,只能是位置参数(不能是关键字参数):
- In : divmod(3, 2)
- Out: (1, 1)
- In : divmod(x=3, y=2)
- ---------------------------------------------------------------------------
- TypeError Traceback (most recent call last)
- <ipython-input-28-6668f56246b2> in <module>
- ----> 1 divmod(x=3, y=2)
- TypeError: divmod() takes no keyword arguments
如果使用关键字参数的方式,会报错。当然这个错有点莫名其妙。为什么要搞Positional-Only呢?就是强制使用者用位置参数!
再看一个例子(bytes):
- In [68]: bytes??
- Init signature: bytes(self, /, *args, **kwargs)
- Docstring:
- bytes(iterable_of_ints) -> bytes
- bytes(string, encoding[, errors]) -> bytes
- bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
- bytes(int) -> bytes object of size given by the parameter initialized with null bytes
- bytes() -> empty bytes object
- ...
- In : bytes('哈哈', 'utf-8')
- Out: b'\xe5\x93\x88\xe5\x93\x88'
- In : bytes('哈哈', encoding='utf-8')
- Out: b'\xe5\x93\x88\xe5\x93\x88'
虽然bytes也有/,但是它只约束了左边的参数(这里只有一个self),之后的encoding可以用位置参数,也能用关键字参数。
通过PEP 570,我们写的Python代码也可以支持了。你可以这样写:
- def name(p1, p2, /, p_or_kw, *, kw):
- def name(p1, p2=None, /, p_or_kw=None, *, kw):
- def name(p1, p2=None, /, *, kw):
- def name(p1, p2=None, /):
- def name(p1, p2, /, p_or_kw):
- def name(p1, p2, /):
在 Python 3.8 时我们就能使用这个新语法啦。现在可以通过 PEP里面的几个简单例子,感受一下它的用法,期待哟
好了,就给大家介绍到这里,希望对大家有所帮助!