今天 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: 10fib(1) = 1fib(2) = 1fib(3) = 2fib(4) = 3fib(5) = 5fib(6) = 8fib(7) = 13fib(8) = 21fib(9) = 34fib(10) = 55Out: 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进程AIn : from multiprocessing import shared_memoryIn : a = shared_memory.ShareableList([1, 'a', 0.1])In : aOut: ShareableList([1, 'a', 0.1], name='psm_d5d6ba1b') # 注意name# IPython进程B(另外一个终端进入IPython)In : from multiprocessing import shared_memoryIn : b = shared_memory.ShareableList(name='psm_d5d6ba1b') # 使用name就可以共享内存In : bOut: ShareableList([1, 'a', 0.1], name='psm_d5d6ba1b')New importlib.metadata module
使用新的 importlib.metadata 模块可以直接读取第三方包的元数据:
In : from importlib.metadata import version, files, requires, distributionIn : 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.versionOut: '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_pointsOut:[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
新闻热点
疑难解答