独家 | Python 3.10发布——你应该知道的五大新特性
作者:Varun Singh 翻译:欧阳锦 校对:王可汗 本文约1700字,建议阅读5分钟
本文为大家介绍了新版本Python的新特性。
图片来源:Canva——由Varun Singh编辑
# I am coding in Python and this is first line ;)
my_list = ["Hello", "Python!"
print(my_list)
File "my_precious.py", line 3
print(my_list)
^
SyntaxError: invalid syntax
File "my_precious.py", line 2
news = ["Hello", "Python!"
^
SyntaxError: '[' was never closed
# missing_comma.py
dc_characters = {
1: "Superman" # Comma missing
2: "Batman",
3: "Joker"
}
....
Output:
File "dc_characters.py", line 4
10: "October"
^^^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?
# Before Python 3.10 Release
from typing import Union
def f(list: List[Union[int, str]], param: Optional[int]):
pass
# In Python 3.10 Release
def f(list: List[int | str], param: int | None):
pass
# Calling the function
f([1, “abc”], None)
int | str == typing.Union[int, str]
typing.Union[int, int] == int
int | int == int
# Before Python 3.10 Release
with (open("a_really_long_foo") as foo,
open("a_really_long_bar") as bar):
pass
Traceback (most recent call last):
File "", line 1, in <module>
File "demo.py", line 19
with (open("a_really_long_foo") as foo,
^
SyntaxError: invalid syntax
# After Python 3.10 Release
from contextlib import contextmanager
def f(x):
try:
yield x
finally:
pass
# Example 1
with f('c') as a,
f('a') as b:
pass
# Example 2
with f('c') as a,
f('a') as b,
f('a') as c:
pass
# Before Python 3.10
UserInfo = tuple[str, int]
# In Python 3.10
from typing import TypeAlias
Card: TypeAlias = tuple[str, str]
Deck: TypeAlias = list[Card]
# Before Python 3.10
names = ["Tom", "Harry", "Jessica", "Robert", "Kevin"]
numbers = ["21024", "75978", "92176", "75192", "34323"]
list(zip(names, numbers))
.....
Output:
[ ]
# Before Python 3.10
names = ["Tom", "Harry", "Jessica", "Robert"] # Kevin is missing
numbers = ["21024", "75978", "92176", "75192", "34323"]
# Zipping using zip()
list(zip(names, numbers))
......
Output
[(Tom, 21024), (Harry, 75978), (Jessica, 92176), (Robert, 75192)]
# In Python 3.10
names = ["Tom", "Harry", "Jessica", "Robert"] # Kevin is missing
numbers = ["21024", "75978", "92176", "75192", "34323"]# Zipping using zip() with additional Parameter strict=True
list(zip(names, numbers, strict=True))....
Output:
Traceback (most recent call last):
File "
" , line 1, inValueError: zip() argument 2 is shorter than argument 1
此版本有很多错误修复以及其他一些小更新,如果您有兴趣,可以在官方发布页面上找到。
原文标题:
Python 3.10 Released — Top 5 New Features You should Know
原文链接:
https://varun-singh-01.medium.com/python-3-10-released-top-5-new-features-you-should-know-bf968ac99230
编辑:于腾凯
译者简介
欧阳锦,一名在埃因霍温理工大学就读的硕士生。喜欢数据科学和人工智能相关方向。欢迎不同观点和想法的交流与碰撞,对未知充满好奇,对热爱充满坚持。
翻译组招募信息
工作内容:需要一颗细致的心,将选取好的外文文章翻译成流畅的中文。如果你是数据科学/统计学/计算机类的留学生,或在海外从事相关工作,或对自己外语水平有信心的朋友欢迎加入翻译小组。
你能得到:定期的翻译培训提高志愿者的翻译水平,提高对于数据科学前沿的认知,海外的朋友可以和国内技术应用发展保持联系,THU数据派产学研的背景为志愿者带来好的发展机遇。
其他福利:来自于名企的数据科学工作者,北大清华以及海外等名校学生他们都将成为你在翻译小组的伙伴。
点击文末“阅读原文”加入数据派团队~
转载须知
如需转载,请在开篇显著位置注明作者和出处(转自:数据派ID:DatapiTHU),并在文章结尾放置数据派醒目二维码。有原创标识文章,请发送【文章名称-待授权公众号名称及ID】至联系邮箱,申请白名单授权并按要求编辑。
发布后请将链接反馈至联系邮箱(见下方)。未经许可的转载以及改编者,我们将依法追究其法律责任。
点击“阅读原文”拥抱组织