前几天 Charles Leifer 写了一篇 《New features planned for Python 4.0》,来计划 Python4.0 的新鲜的功能。在这里简单记录一下他的畅想。


With the release of Python 3.8 coming soon, the core development team has asked me to summarize our latest discussions on the new features planned for Python 4.0, codename "ouroboros: the snake will eat itself". This will be an exciting release and a significant milestone, many thanks to the hard work of over 100 contributors.

  • After heated debate on the mailing list, the 79-character line limit prescribed by PEP8 will be updated. IDE users all over the world will now be able to take advantage of their 30" ultra-wide 4K monitors, as the recommended line length will be increased to 89.5 characters (this was a compromise with the 100-character lobby, the decision being to split the difference).
  • All new libraries and standard lib modules must include the phrase "for humans" somewhere in their title.
  • Finally, a new string-type for the masses, Python 4.0 will feature "z-strings": C-style NULL terminated bytestrings. Just prefix your string like so, z'my string' and Python will automatically ensure it is NULL-terminated. Note: the new z-strings cannot be used with any of the existing APIs that take string arguments - they must first be decoded to unicode strings or cast to bytes.
  • Type-hinting has been extended to provide even fewer tangible benefits. This new and "nerfed" type hinting will be called type whispering.
  • Fuck it we're going to just vendor libuv to provide the event loop for Twisted asyncio.
  • You can now use the async keyword before every single other keyword in the Python language, and we encourage you to async do so. There's nothing wrong with cargo-culting some magic keywords all over the place -- you want to go fast, don't you?
  • In addition to namedtuple and dataclasses (3.7), Python 4.0 will include several new thousand line decorator-hacks to implement simple struct types.
  • The GIL has been removed.
  • Just kidding! Instead we've been focusing all our effort on making it easier to juggle multiple interpreter data-structures within a single thread. No, no, you can thank us later!
  • The bytes-vs-str thing kept many of us employed as we had convinced our companies they needed to upgrade to Python 3. In the same spirit, we are excited to announce that there will now be two int types -- int will be a 32-bit signed integer, and long will be a 64-bit signed integer. But before you say, "hey, they did that in Python 2!", we'd like to add that you can no longer use int anywhere, and will need to convert them all to long.
  • Based on the overwhelming success of the 2to3 utility, we plan to release a 3to4 tool that will automatically convert your code to utilize these exciting new features.

With much sadness, the following features did not make the cut:

  • After attempting to rewrite portions of the interpreter with Rust, nobody could figure out how to disable the borrow-checker, so we gave up.
  • No switch statement (and yes, yes, I know you can use a dict to do dispatching).
  • concurrent.Pasts and concurrent.Present will not be merged in time for this release, but hey, we've got futures, haven't we?
  • Since nobody understands how twisted asyncio works, we are unable to offer any improvements at this time. The PSF now recommends all new projects to use gevent.
  • Unfortunately we will not be able to offer any improvements to the packaging "situation".

We look forward to this release, and will do everything in our power to ensure it takes us several minor versions before it is even remotely usable.

Take heart! Remember the python motto:

What is dead can never die


Python 3.8 发布在即,核心开发者团队让我总结一下最近讨论的 Python 4.0 预计推出的新功能,代码名为“ Ouroboros:自噬蛇”。Python 4.0 是大家翘首以盼的新版本,也将成为 Python 历史上的重大里程碑,在此感谢为之努力奋斗的 100 多位贡献者。

  • 经过邮件列表上的论战,PEP8 里的一行 79 个字符的限制将增加至一行 89.5 个字符。IDE 用户终于可以充分利用 30寸 超宽 4K 显示器的优势啦!这是我们对每行 100 个字符这一呼声的妥协,大家各退一步。
  • 所有新的支持库与标准库模块的标题中必须包含 For Humans(『以人为本』)。
  • Python 4.0 终将推出一种新的字符串类型,『z-strings』, 这是一种 C 语言风格的空终止字节字符串。只要在字符串加上前缀 z,如 z'my string',Python 就会自动将它转换成空终止字符串(NULL-terminated)。注意:z-strings 不能用于现有需要获取字符串参数的 API,应该先将它解码为 Unicode 字符串,或转换为字节(bytes)。
  • Type-hinting(类型提示)扩展将提供一些更实用的功能。新推出的简化版类型提示将被称为 Type Whispering(类型低语)。
  • 他码弟,我们要用 libuv 为 Twisted acyncio 提供事件循环(Event Loop)支持。
  • Python 4.0 将支持在单一关键词前使用 async 关键词,并且鼓励使用 async do so 这种方式。用 Cargo-Culting[注1] 方式编程,让魔法关键词出现在所有地方也没什么不好,大家都希望程序能跑得更快,是不是?
  • 在 Python 3.7 版的 namedtupledataclasses 基础上,Python 4.0 将增加几个千行 Decorator-Hack,用以实现简单构建类型。
  • 全局解释锁(GIL)已经被移除了。
  • 开玩笑的!反之,我们一直努力,希望实现在一个线程里,处理多个解释器数据结构更容易。别客气,以后再感谢我们吧!
  • 需要将 bytes 转为 str 这件事,让众多被我们说服从 Python 2.x 升级到 3.x 的公司为我们提供了不少工作机会。本着同样的精神,很高兴告诉大家,今后会有两个 int 类型,其中,int 是 32 位带符号的整数,long 是 64 位带符号的整数。可能会有人说 “Python 2 不就是这样吗?”,对于这点,我们要告诉大家,以后再也用不了 int 了,要把它们都转换为 long
  • 鉴于,Python 2 升级 3 的工具大获成功,我们计划推出 Python 3 升级 4 的工具,可以自动将代码转换为上述新特性。

很遗憾,以下特性未能入选 Python 4.0 升级计划:

  • 我们尝试过用 Rust 重写解释器的部分功能,但没人知道怎么禁用 borrow-cheker,所以只好放弃了。
  • 没有 switch 语句,不过,你可以用 dict
  • concurrent.Pasts(过去)concurrent.Present(现在) 没有合并,不过,没关系,我们还有 future(未来),是不是?
  • 没人能搞懂 Twisted asyncio 如何运作,届时,将不会对这方面的内容有所改进。Python 软件基金会建议所有新项目都使用 gevent
  • 很抱歉,我们没有对打包“环境”做出任何改进。

要有信心!记住 Python 的宣言:


注释 1:程序员不了解他们试图解决的错误或明显的解决方案的症状。


最开始阅读的时候我天真的认为这将会在 Python4 中出现。不管如何解释 GIL 的存在一定是 Python 的短板。抛开 IO 密集型的任务,在计算密集型的任务下,Python 的表现可谓十分之差。与此同时 Ruby 的社区正在努力Ruby3,PHP8 也在计划增加 JIT,这些也说明目前的趋势是增强计算性能。
Eric Snow 在今年的 PyCon(PyCon2019)上宣布,去除 GIL 将会是未来的 Python 社区的方向之一。

我们有理由相信,未来会变的更好,但一定的是如果不做出一些改变一定没有未来,也正是 『What is dead can never die』。

1 + 9 =