浅析PEP570新语法: 只接受位置参数
作者:小明 时间:2022-12-22 09:36:28
最近 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里面的几个简单例子,感受一下它的用法,期待哟
好了,就给大家介绍到这里,希望对大家有所帮助!
来源:https://zhuanlan.zhihu.com/p/64246881
标签:PEP570,语法,参数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Vuex的安装、搭建及案例详解
2024-05-29 22:20:22
![](https://img.aspxhome.com/file/2023/0/123180_0s.png)
不拘小节的中文字体设计
2009-05-21 10:44:00
vue2.0中set添加属性后视图不能更新的解决办法
2024-04-28 10:55:18
![](https://img.aspxhome.com/file/2023/6/133066_0s.png)
vue获取input值的三种常用写法
2024-04-28 09:27:06
![](https://img.aspxhome.com/file/2023/0/132980_0s.gif)
Python使用pylab库实现绘制直方图功能示例
2022-12-07 04:48:56
![](https://img.aspxhome.com/file/2023/1/120941_0s.png)
pandas中对文本类型数据的处理小结
2022-06-25 06:39:16
![](https://img.aspxhome.com/file/2023/7/70787_0s.png)
关于Kotlin中SAM转换的那些事
2022-02-09 15:14:38
Django框架文件上传与自定义图片上传路径、上传文件名操作分析
2022-06-09 12:44:28
Python新手如何理解循环加载模块
2022-10-09 00:12:53
使用VSCode如何从github拉取项目的实现
2023-02-02 10:32:35
![](https://img.aspxhome.com/file/2023/2/112072_0s.jpg)
pandas 根据列的值选取所有行的示例
2023-10-13 16:19:38
PHP实现的AES双向加密解密功能示例【128位】
2023-09-30 14:52:51
深入理解Python 多线程
2021-03-25 05:25:28
![](https://img.aspxhome.com/file/2023/1/110221_0s.png)
Go处理JSON数据的实现
2024-02-23 07:44:42
使用Python编程分析火爆全网的鱿鱼游戏豆瓣影评
2023-10-11 00:35:26
![](https://img.aspxhome.com/file/2023/7/75227_0s.png)
Centos 6.5系统下编译安装PHP 7.0.13的方法
2023-06-07 13:04:26
Python遍历字典方式就实例详解
2021-02-16 08:50:58
Python获取对象属性的几种方式小结
2022-05-27 18:09:42
Python求离散序列导数的示例
2022-10-04 11:18:52
![](https://img.aspxhome.com/file/2023/7/63987_0s.jpg)
Python定义一个跨越多行的字符串的多种方法小结
2022-08-04 03:34:27
![](https://img.aspxhome.com/file/2023/0/115070_0s.jpg)