Python 3.10 明年发布,看看都有哪些新特性?
阅读本文大概需要 2 分钟。
推荐三个网站
1.Python进阶知识: http://python.iswbm.com
2.Python魔法技巧: http://magic.iswbm.com
3.PyCharm 手册: http://pycharm.iswbm.com
突出显示Python 3.10中的功能
(1) 二进制表示中的频率为1
# Positive integer
>>> num = 108
# Let's first get the binary representation of num
>>> bin(num)
'0b1101100'
>>> num.bit_count()
4
# Negative integer
>>> num = -108
>>> bin(num)
'-0b1101100'
>>> num.bit_count()
4
# Under the hood
>>> bin(num).count('1')
(2) 压缩将是"严格的"
>>> list(zip(['A', 'B', 'C', 'D'], ['Apple', 'Ball', 'Cat']))
[('A', 'Apple'), ('B', 'Ball'), ('C', 'Cat')]
>>> list(zip(['A', 'B', 'C', 'D'], ['Apple', 'Ball', 'Cat'], strict=True))
Traceback (most recent call last): ...ValueError: zip() argument 1 is longer than argument 2
(3) 字典的只读视图
>>> fruits = {'Mangos': 12, 'Figs': 100, 'Guavas': 3, 'Kiwis': 70}
>>> keys = fruits.keys()
>>> values = fruits.values()
>>> list(keys)
['Mangos', 'Figs', 'Guavas', 'Kiwis']
>>> del fruits['Figs']
>>> del fruits['Guavas']
>>> print (list(keys), list(values))
['Mangos', 'Kiwis'] [12, 70]
# returns a read-only proxy of the original dictionary
>>> values.mapping
mappingproxy({'Mangos': 12, 'Figs': 100, 'Guavas': 3, 'Kiwis': 70})
>>> values.mapping['Guavas']
3
(4) 消除一些向后兼容性
>>> from collections import ABC_Name
DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working
评论