Python3.8 特性介绍

智码先锋说
• 阅读 3100

简介

Python3.8 已经发布了, 官方文档看这里
What’s New In Python 3.8.

介绍一些 Python3.8 中的新特性.

海象表达式 :=

新的语法 := 将给变量赋值, 这个变量是更大的表达式的一部分.

if (n := len(a)) > 10:
  print(f"List is too long ({n} elements, expected <= 10)")

用在 if 判断中, 避免调用 len() 两次.

discount = 0.0
if (mo := re.search(r'(\d+)% discount', advertisement)):
  discount = float(mo.group(1)) / 100.0

正则表达式匹配和获取结果的时候.

# Loop over fixed length blocks
while (block := f.read(256)) != '':
  process(block)

用在 while 循环中, 可以同时取值, 并判断是否为空.

[clean_name.title() for name in names
 if (clean_name := normalize('NFC', name)) in allowed_names]

用在列表推导中.

完整介绍看 PEP 572.

仅位置参数 /

新的函数参数语法 / 指明有些函数参数必须被指定为位置参数, 不能被用作关键字参数.

def f(a, b, /, c, d, *, e, f):
  print(a, b, c, d, e, f)

在上面的例子中, a 和 b 是仅位置参数, c 和 d 既可以是位置参数又可以是关键字参数,
e 和 f 必须是关键字参数.

>>> def f(a, b, /, **kwargs):
...     print(a, b, kwargs)
...
>>> f(10, 20, a=1, b=2, c=3)         # a and b are used in two ways
10 20 {'a': 1, 'b': 2, 'c': 3}

仅位置参数的参数名在 **kwargs 中仍旧可用.

class Counter(dict):
  def __init__(self, iterable=None, /, **kwds):
    # Note "iterable" is a possible keyword argument

完整介绍看 PEP 570.

f-strings 说明符 =

f-strings 增加了 = 说明符, f'{expr=}' 会被扩展为表达式的文本,
加上一个等号, 和一个执行表达式的结果.

>>> user = 'eric_idle'
>>> member_since = date(1975, 7, 31)
>>> f'{user=} {member_since=}'
"user='eric_idle' member_since=datetime.date(1975, 7, 31)"

启动异步 REPL

使用 python -m asyncio 启动一个异步的 REPL,
可以直接在 top-level 级别使用 await,
不用在封装到函数中了.

unittest 支持异步

import unittest

class TestRequest(unittest.IsolatedAsyncioTestCase):

    async def asyncSetUp(self):
        self.connection = await AsyncConnection()

    async def test_get(self):
        response = await self.connection.get("https://example.com")
        self.assertEqual(response.status_code, 200)

    async def asyncTearDown(self):
        await self.connection.close()


if __name__ == "__main__":
    unittest.main()
点赞
收藏
评论区
推荐文章
小果果学长 小果果学长
4年前
【编译原理】语义分析S属性定义的自下而上计算
目录一、实验目的(about:blank%E4%B8%80%E3%80%81%E5%AE%9E%E9%AA%8C%E7%9B%AE%E7%9A%84)二、实验任务(about:blank%E4%BA%8C%E3%80%81%E5%AE%9E%E9%AA%8C%E4%BB%BB%E5%8A%A1)三、实验原理(about:blank%E
Stella981 Stella981
3年前
Apollo
现在,各个服务端都启动完毕,我们可以开始研究client端怎么使用apolloclient了。具体请参考https://github.com/ctripcorp/apollo/wiki/Java%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%971)普通jav
Stella981 Stella981
3年前
Apollo使用文档(Java)
1、apolo的github上的文档很全,大家可以去github上查看下面是java客户端使用文档https://github.com/ctripcorp/apollo/wiki/Java%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97(https://www.oschin
Stella981 Stella981
3年前
Git 克隆远程仓库到本地
Git克隆远程仓库到本地参考$gitclonehelphttps://gitscm.com/book/zh/v2/Git%E5%9F%BA%E7%A1%80%E8%8E%B7%E5%8F%96Git%E4%BB%93%E5%BA%93当前目录/e/mozq/0
Wesley13 Wesley13
3年前
mysql事务和事务隔离机制
阅读文本大概需要3分钟。http://arthornye.github.io/2018/mysql/mysql%E4%BA%8B%E5%8A%A1%E5%92%8C%E4%BA%8B%E5%8A%A1%E9%9A%94%E7%A6%BB%E6%9C%BA%E5%88%B6在学习mysql的事务隔离机制的过程中,对mysql的会话和事务的概念有点模糊不
Wesley13 Wesley13
3年前
Java 入门进阶
Java入门进阶發表於20150416http://xielong.me/2015/04/16/%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%B8%88%E5%85%A5%E9%97%A8%E4%B8%8E%E8%BF%9B%E9%98%B6Java%E7%89%88/
Stella981 Stella981
3年前
SVN基本命令
SVN基本命令从远程检出代码svncohttp://路径(目录或文件的全路径)(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2F%E8%B7%AF%E5%BE%84%28%E7%9B%AE%E5%BD%95%E6%88%96%E6%96%87%E4%
Wesley13 Wesley13
3年前
Java三大特性
_Java面向对象编程三大特性:封装继承多态(https://snailclimb.gitee.io/javaguide//docs/java/Java%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86?id_11java%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e7%bc%96%e
Wesley13 Wesley13
3年前
2、Libgdx配置你的开发环境(Eclipse,Intellij IDEA,NetBeans)
(原文:http://www.libgdx.cn/topic/13/2libgdx%E9%85%8D%E7%BD%AE%E4%BD%A0%E7%9A%84%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83eclipseintellijideanetbeans(https://www.oschina.net/action/G
Easter79 Easter79
3年前
SVN基本命令
SVN基本命令从远程检出代码svncohttp://路径(目录或文件的全路径)(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2F%E8%B7%AF%E5%BE%84%28%E7%9B%AE%E5%BD%95%E6%88%96%E6%96%87%E4%
Wesley13 Wesley13
3年前
Eclipse插件开发调试篇
介绍本文介绍如何定制一种适合自己项目调试的方式。扩展点查看原文:http://surenpi.com/2015/08/13/eclipse%e6%8f%92%e4%bb%b6%e5%bc%80%e5%8f%91%e8%b0%83%e8%af%95%e7%af%87/(https://www.oschina.net/