Python 3.8 即将发布了,有哪些新特性和需要注意的更新?

准备升级了....

python 3.8 release date

3.8.0 candidate 2: Monday, 2019-10-07 (if necessary)
3.8.0 final: Monday, 2019-10-14 (assuming a single release candidate)

Features for 3.8 Some of the notable features of Python 3.8 include:

  • PEP 570, Positional-only arguments
  • PEP 572, Assignment Expressions
  • PEP 574, Pickle protocol 5 with out-of-band data
  • PEP 578, Runtime audit hooks
  • PEP 587, Python Initialization Configuration
  • PEP 590, Vectorcall: a fast calling protocol for CPython
  • Typing-related: PEP 591 (Final qualifier), PEP 586 (Literal types), and PEP 589 (TypedDict)
  • Parallel filesystem cache for compiled bytecode
  • Debug builds share ABI as release builds
  • f-strings support a handy = specifier for debugging
  • continue is now legal in finally: blocks
  • on Windows, the default asyncio event loop is now ProactorEventLoop
  • on macOS, the spawn start method is now used by default in multiprocessing
  • multiprocessing can now use shared memory segments to avoid pickling costs between processes
  • typed_ast is merged back to CPython
  • LOAD_GLOBAL is now 40% faster
  • pickle now uses Protocol 4 by default, improving performance
  • There are many other interesting changes, please consult the "What's New" page in the documentation for a full list.

Python 3.8 即将发布了,有哪些新特性和需要注意的更新?

59 回复

即将不能用于完成时


Python 3.8 确实带来了一些很实用的新特性,我挑几个重点的说说。

1. 海象运算符 (:=) 这个可能是最受关注的。它允许你在表达式内部进行赋值,让一些代码更紧凑。比如在循环或条件判断中需要重复计算同一个值时:

# 传统写法
n = len(my_list)
if n > 10:
    print(f"List is too long ({n} elements)")

# 使用海象运算符
if (n := len(my_list)) > 10:
    print(f"List is too long ({n} elements)")

while循环里读取数据时特别有用:

while (chunk := file.read(8192)) != b'':
    process(chunk)

2. 仅位置参数 (/) 在函数定义中,使用/可以强制它之前的参数必须通过位置传递,不能用作关键字参数。这在你设计一些API,想确保参数顺序或避免参数名冲突时很有用。

def f(a, b, /, c, d):
    return a + b + c + d

f(1, 2, c=3, d=4)  # 正确
f(1, b=2, c=3, d=4)  # 报错:b不能使用关键字参数

3. f-string 支持 = 用于调试 现在f-string里可以直接用=来输出变量名和它的值,调试打印的时候省事多了。

user = 'john'
print(f'{user=}')  # 输出:user='john'

4. 改善的pickle协议 默认的pickle协议版本从3升到了4,支持一些新类型的序列化,并且性能有提升。不过要注意向下兼容性。

需要注意的更新:

  • continuefinally块中的行为变化:在finally块里使用continue现在会触发RuntimeWarning,未来版本可能会直接报错。检查下你的代码里有没有这种写法。
  • 一些弃用警告:比如collections.abc里的一些别名开始弃用,用collections.abc.Mapping代替collections.Mapping

总的来说,3.8的更新挺实在的,尤其是海象运算符和f-string的增强,用好了能让代码更简洁。升级前最好用python -Wa跑一下你的测试,看看有没有新的警告。

建议:升级前务必在测试环境充分验证。

哎。。。

准备升级…

官网不是还是 3.7.4 吗?

要那么新干嘛,还在用 3.6🐶

哪位总结一下有什么好用的特性

上次看到的 final 日期还是 10.28

3.6 升级到 3.7 的时候感觉性能提升明显,平时用的脚步速度提升了约 15%。这次看看了。

还在用 2.7 的默默路过

中文“了”字结尾不一定是完成时(中文语法本身没有时态)。

“好了吗?快点出来”
“快好了,快好了”

只有赋值表达式是重大改动吧,,

子解释器上了吗

是的… 特性的话, [pep 572]( https://www.zhihu.com/question/274823057) 让 python 之父很恼火…

multiple interpreters ? 这里提案在 [pep 554]( https://www.python.org/dev/peps/pep-0554/) 我看了下 3.9 版本才会发

感觉 typing hints 再努力努力,python 就可以考虑分支为动态脚本、静态编译两个实现了

有哪个大牛去提个 PEP ?

标注时间了呀. 下周一可以试试了.

升吧.

不大可能. 可以用 mypy.

不太乐观,TypeScript 类型更强,但是也没静态编译到原生二进制吧

我的看法:3.8 也就更新了这两件值得注意的事情

1、加强了 Type Hinting,增加了 Duck Type Hinting 的支持。
2、assignment operation。

小打小闹而已。

嗯, 日常更新. 另外 assignment operation 我还在考虑用不用…

我觉得这就是哪天哪个大牛心血来潮搞一下的事情…

没上

会有这种性能提升?

能用还是尽量用,确实让代码更简洁,毕竟连 C++ 都有类似语法了

minits 项目正在做 ts 的静态编译器

是啊…考虑中.

Duck Type Hinting 是指?

主要看 changelog 里有没有 performance 提升,当然也和脚本具体内容有关,可能正好是作用到那段代码了,因为平时我都带计时,所以很明显。

python 这越来越复杂,传个参都整这么多花样有必要么,JS 这么简单写起来还比 py 舒服

场景不一样. 不能简单这样比较. 各有各的优劣.

我也觉得 pep572 有点画蛇添足。社区那么多人反对,主要理由也是增加的便捷性不大,带来的复杂度提升过冗。

那 Golang 和 C++ 也都在画蛇添足了
这一期我们稍微聊过一下 572,主要的槽点我认为在于语法没有强制限定使用范围: https://www.pythonhunter.org/episodes/5


可是你们还得学 TypeScript 啊,,JS 的很多库(比如 Vue )都用 TypeScript 写了,,

我写 js 也全是用 typescript 啊,觉得不难,js 本质是个简单的语言。哪怕在底层 py 的复杂性都完暴 js,元类什么的我以前研究完就觉得过度设计。

对这点龟叔大概的意思是:有这个方法,你可以不用啊。然后,社区就反对,这不 Pythonic。

简单说,就是好处坏处都不大,不太符合奥卡姆原理。

前几天看完了 what’s new,感觉不像 3.6 和 3.7 有非常吸引人升级的特性…

疑车无据


元类是给库、框架开发人员用的,我们不需要深究

正是因为 Python 有这些复杂的语法机制,Python 的用户 API 才能这么易用,,

如果 Python 没有__xxx__那一堆魔法函数,numpy 就不会出现,,没有 numpy 的话 Python 就不可能在数值计算、数据分析、机器学习、深度学习这些领域流行起来

Python 里的 ORM 也依赖元类实现,,不知道 JS 中的 ORM 相比于 Python 中的 ORM 使用起来怎么样,,没有对比过,,

Java
> 百度为您找到相关结果约 69,100,000 个
## python
> 百度为您找到相关结果约 38,500,000 个
## c++
> 百度为您找到相关结果约 59,700,000 个
## golang
> 百度为您找到相关结果约 24,600,000 个

PEP 572 还是能够接受的,,毕竟使用场景比较明确,,

PEP 505 – None-aware operators 就不能接受了,,还好 Deferred 了,希望不要通过



最好再统计下 github、StackOverflow、google、quora、知乎等多种数据来源综合下,,单一数据说服力不够,,

thanks,这个不错

LOAD_GLOBAL is now 40% faster
这不算巨大提升吗…调用函数都要 LOAD GLOBAL 的

(学不动了

中文没有时态不代表了不能表完成啊
就举的例子里,“好”和“好了”的区别就是一个是原型一个是表完成啊,你把“好了”封装成名词做句子成分当然就看不出来了。

同感!

弃坑了,始于 2.7 止于 3.6

multiprocessing can now use shared memory segments to avoid pickling costs between processes 这个好

“即将发布”是一个确定的状态,已经发生了,用“了”没毛病。

一定要用 3.6+,提升太明显了!

https://docs.python.org/3.8/whatsnew/3.8.html#optimizations 看了下优化部分, 还是挺明显的.

Improved performance of operator.itemgetter() by 33%. Optimized argument handling and added a fast path for the common case of a single non-negative integer index into a tuple (which is the typical use case in the standard library).


Sped-up field lookups in collections.namedtuple(). They are now more than two times faster, making them the fastest form of instance variable lookup in Python.


The list constructor does not overallocate the internal item buffer if the input iterable has a known length (the input implements len). This makes the created list 12% smaller on average.


Reduced an overhead of converting arguments passed to many builtin functions and methods. This sped up calling some simple builtin functions and methods up to 20–50%.


LOAD_GLOBAL instruction now uses new “per opcode cache” mechanism. It is about 40% faster now.

为什么这个测试结果有这么多不一致的地方…

回到顶部