今天 Python3.8 发布啦,它是 Python2 终结前最后一个大版本,我们一起看看这个版本都添加了那些新功能和特性。

PEP 572: Assignment Expressions

PEP 572 的标题是赋值表达式,也叫做「命名表达式」,不过它现在被广泛的别名是「海象运算符」(The Walrus Operator)。因为:= 很像海象「眼睛小,长着两枚长长的牙」这个特点 ^_^。

具体内容可以看我之前写的文章: PEP572: 海象运算符 ,在这里给大家展示个通过用 PEP 572 改写的一行实现斐波那契数列的例子:

In :  (lambda f: f(f, int(input('Input: ')), 1, 0, 1))(lambda f, t, i, a, b: print(f'fib({i}) = {b}') or t == i or f
   ...: (f, t, i + 1, b, a + b))
Input: 10
fib(1) = 1
fib(2) = 1
fib(3) = 2
fib(4) = 3
fib(5) = 5
fib(6) = 8
fib(7) = 13
fib(8) = 21
fib(9) = 34
fib(10) = 55
Out: True

基于 Raymond Hettinger 版本改写:

In : [(t:=(t[1], sum(t)) if i else (0,1))[1] for i in range(10)]
Out: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

PEP 570: Python Positional-Only parameters

PEP 570 说白了就是强制使用者用位置参数

具体的可以看我之前写的文章: PEP570 新语法:只接受位置参数

PEP 578: Python Runtime Audit Hooks

现在可以给 Python 运行时添加审计钩子:

In : import sys
...: import urllib.request
...:
...:
...: def audit_hook(event, args):
...:     if event in ['urllib.Request']:
...:         print(f'Network {event=} {args=}')
...:
...: sys.addaudithook(audit_hook)

In : urllib.request.urlopen('https://httpbin.org/get?a=1')
Network event='urllib.Request' args=('https://httpbin.org/get?a=1', None, {}, 'GET')
Out: <http.client.HTTPResponse at 0x10e394310>

目前支持审计的事件名字和 API 可以看 PEP 文档 (延伸阅读链接 2),urllib.Request是其中之一。另外还可以自定义事件:

In : def audit_hook(event, args):
...:     if event in ['make_request']:
...:         print(f'Network {event=} {args=}')
...:

In : sys.addaudithook(audit_hook)

In : sys.audit('make_request', 'https://baidu.com')
Network event='make_request' args=('https://baidu.com',)

In : sys.audit('make_request', 'https://douban.com')
Network event='make_request' args=('https://douban.com',)

Multiprocessing shared memory

可以跨进程直接访问同一内存 (共享):

# IPython进程A
In : from multiprocessing import shared_memory

In : a = shared_memory.ShareableList([1, 'a', 0.1])

In : a
Out: ShareableList([1, 'a', 0.1], name='psm_d5d6ba1b') # 注意name
# IPython进程B(另外一个终端进入IPython)
In : from multiprocessing import shared_memory

In : b = shared_memory.ShareableList(name='psm_d5d6ba1b')  # 使用name就可以共享内存

In : b
Out: ShareableList([1, 'a', 0.1], name='psm_d5d6ba1b')

New importlib.metadata module

使用新的importlib.metadata模块可以直接读取第三方包的元数据:

In : from importlib.metadata import version, files, requires, distribution

In : version('flask')
Out: '1.1.1'

In : requires('requests')
Out:
['chardet (<3.1.0,>=3.0.2)',
 'idna (<2.9,>=2.5)',
 'urllib3 (!=1.25.0,!=1.25.1,<1.26,>=1.21.1)',
 'certifi (>=2017.4.17)',
 "pyOpenSSL (>=0.14) ; extra == 'security'",
 "cryptography (>=1.3.4) ; extra == 'security'",
 "idna (>=2.0.0) ; extra == 'security'",
 "PySocks (!=1.5.7,>=1.5.6) ; extra == 'socks'",
 'win-inet-pton ; (sys_platform == "win32" and python_version == "2.7") and extra == \'socks\'']

In : dist = distribution('celery')

In : dist.version
Out: '4.3.0'

In : dist.metadata['Requires-Python']
Out: '>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'

In : dist.metadata['License']

In : dist.entry_points
Out:
[EntryPoint(name='celery', value='celery.__main__:main', group='console_scripts'),
 EntryPoint(name='celery', value='celery.contrib.pytest', group='pytest11')]

In : files('celery')[8]
Out: PackagePath('celery/__init__.py')

In : dist.locate_file(files('celery')[8])
Out: PosixPath('/Users/dongweiming/test/venv/lib/python3.8/site-packages/celery/__init__.py')

functools.cached_property

缓存属性 (cached_property) 是一个非常常用的功能,很多知名 Python 项目都自己实现过它,现在终于进入版本库了。

具体的可以看我之前写的文章: functools.cached_property(Python 3.8)

functools.lru_cache 作为装饰器时可以不加参数

lru_cache装饰器支持max_sizetyped2 个参数,如果对默认参数不敏感,过去只能这么用 (需要空括号):

In : @lru_cache()
...: def add(a, b):
...:     return a + b
...:

从 3.8 开始可以直接作为装饰器,而不是作为返回装饰器的函数 (不加括号):

In : @lru_cache
...: def add(a, b):
...:     return a + b
...:

就像dataclasses.dataclass,绝大部分场景都是这么用:

@dataclass
class InventoryItem:
    ...

其实dataclass支持多个参数:

def dataclass(cls=None, /, *, init=True, repr=True, eq=True, order=False,
              unsafe_hash=False, frozen=False):

所以这种使用全部缺省值的装饰器工厂用法中,括号反而显得多余了。

Asyncio REPL

具体的可以看我之前写的文章: asyncio REPL(Python 3.8)

F-strings DEBUG

具体的可以看我之前写的文章: 使用 f-strings 调试 (Python3.8)

Async Mock

单元测试模块 unittest 添加了 mock 异步代码的类:

In : import asyncio

In : from unittest.mock import AsyncMock, MagicMock

In : mock = AsyncMock(return_value={'json': 123})
In : await mock()
Out: {'json': 123}

In : asyncio.run(mock())
Out: {'json': 123}

In : async def main(*args, **kwargs):
...:     return await mock(*args, **kwargs)
...:

In : asyncio.run(main())
Out: {'json': 123}

In : mock = MagicMock()  # AsyncMock也可以

In : mock.__aiter__.return_value = [1, 2, 3]

In : async def main():
...:     return [i async for i in mock]
...:

In : asyncio.run(main())
Out: [1, 2, 3]

Generalized iterable unpacking in yield and return

具体的可以看我之前写的文章: Python3.8 对「可迭代解包」的改进

后记

全部更改详情请查阅 Changelog (延伸阅读链接 1),另外推荐阅读延伸阅读链接 3 和 4,都是非常好的补充

延伸阅读

  1. https://docs.python.org/3.8/whatsnew/3.8.html
  2. https://www.python.org/dev/peps/pep-0578/#id11
  3. https://speakerdeck.com/matrixise/whats-new-in-python-3-dot-8
  4. https://realpython.com/python38-new-features/