今天 Python3.8 發(fā)布啦,它是 Python2 終結(jié)前最后一個(gè)大版本,我們一起看看這個(gè)版本都添加了那些新功能和特性。
PEP 572: Assignment Expressions
PEP 572 的標(biāo)題是賦值表達(dá)式,也叫做「命名表達(dá)式」,不過(guò)它現(xiàn)在被廣泛的別名是「海象運(yùn)算符」(The Walrus Operator)。因?yàn)?= 很像海象「眼睛小,長(zhǎng)著兩枚長(zhǎng)長(zhǎng)的牙」這個(gè)特點(diǎn) ^_^。
具體內(nèi)容可以看我之前寫(xiě)的文章: PEP572: 海象運(yùn)算符 ,在這里給大家展示個(gè)通過(guò)用 PEP 572 改寫(xiě)的一行實(shí)現(xiàn)斐波那契數(shù)列的例子:
In : (lambda f: f(f, int(input('Input: ')), 1, 0, 1))(lambda f, t, i, a, b: print(f'fib({i}) = ') 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 版本改寫(xiě):
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 說(shuō)白了就是 強(qiáng)制使用者用位置參數(shù)
具體的可以看我之前寫(xiě)的文章: PEP570 新語(yǔ)法:只接受位置參數(shù)
PEP 578: Python Runtime Audit Hooks
現(xiàn)在可以給 Python 運(yùn)行時(shí)添加審計(jì)鉤子:
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>目前支持審計(jì)的事件名字和 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可以跨進(jìn)程直接訪問(wèn)同一內(nèi)存 (共享):
# IPython進(jìn)程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進(jìn)程B(另外一個(gè)終端進(jìn)入IPython)In : from multiprocessing import shared_memoryIn : b = shared_memory.ShareableList(name='psm_d5d6ba1b') # 使用name就可以共享內(nèi)存In : bOut: ShareableList([1, 'a', 0.1], name='psm_d5d6ba1b')New importlib.metadata module
使用新的 importlib.metadata 模塊可以直接讀取第三方包的元數(shù)據(jù):
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
新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注