当前位置: 首页 > news >正文

湖南网站建设小公司排名/重庆疫情最新消息

湖南网站建设小公司排名,重庆疫情最新消息,java做网站是不是成本更高,站长工具seo综合查询全面解析python3.10发行已经有一段时间了,但是时至今日才开始用上python3.10版本,说实话有点惭愧。下面来记录一下,在Python3.10版本中几个亮眼的语法变更: 带括号的上下文管理器 在以前,我们如果需要打开多个文件&#xff0…

python3.10发行已经有一段时间了,但是时至今日才开始用上python3.10版本,说实话有点惭愧。下面来记录一下,在Python3.10版本中几个亮眼的语法变更:

带括号的上下文管理器

在以前,我们如果需要打开多个文件,我们通常会使用多个with操作,就像这样:

with open("a.txt", "r") as fa:passwith open("b.txt", "w") as fb:pass

在python3.10中,引入了带括号的上下文管理器,我们可以这样写了:

with (open("a.txt", "r") as fa,open("b.txt", "w") as fb
):pass

结构模式匹配

结构模式匹配已以match语句 和具有关联操作的模式的case 语句的形式添加。模式由序列、映射、原始数据类型以及类实例组成。模式匹配使程序能够从复杂的数据类型中提取信息,在数据结构上进行分支,并根据不同形式的数据应用特定的操作。如下:

match subject:case <pattern_1>:<action_1>case <pattern_2>:<action_2>case <pattern_3>:<action_3>case _:<action_wildcard>

你还可以使用|将多个条件组合在一个模式中:

case 401 | 403 | 404:return "Not Allowed"

如果未使用case _匹配,并且上面的case条件都不满足的情况下,将会发生空操作,即返回一个None

用于match匹配的变量,除了数字或字符串之外,还可以是一个元组或者列表,甚至是一个类的对象。使用如下:

  • 元组
def location(points):match points:case (0, 1):print("(0, 1) is the point's location.")case (1, 0):print("(1, 0) is the point's location.")case _:print("other")if __name__ == "__main__":location((0, 1))        # (0, 1) is the point's location.location((1, 0))        # (1, 0) is the point's location.location((1, 1))        # other
  • 列表
def match_case(var):match var:case []:print("var is empty")case [1, 2]:print("var is [1, 2]")case [1, 1]:print("var is [1, 1]")case _:print("other")if __name__ == "__main__":match_case([])          # var is emptymatch_case([1, 2])      # var is [1, 2]match_case([1, 1])      # var is [1, 1]match_case([2, 2])      # other
  • 类对象
class Point(object):x: inty: intdef location(point):match point:case Point(x=0, y=0):print("Origin is the point's location")case Point(x=0, y=y):   # x为0,y为任一值print(f"Y={y} and the point is on the y-axis")case Point(x=x, y=0):   # y为0,x为任一值print(f"X={x} and the point is on the x-axis")case Point():           # x或y没有值,或都没有值的情况print("The point is located somewhere else on the plane.")case _:print('not a plane')
  • 通配符
match test_variable:case ('warning', code, 40):print("A warning has been received.")case ('error', code, _):print(f"An error {code} occurred.")

在上述情况下,test_variable将匹配 (‘error’, code, 100) 和 (‘error’, code, 800)。

  • 守卫
match point:case Point(x, y) if x == y:print(f"The point is located on the diagonal Y=X at {x}.")case Point(x, y):print(f"Point is not on the diagonal.")

结构模式匹配的使用跟Java的switch...case语法有点类似!

|操作符

在之前我们需要对一个参数对多类型的匹配时,我们需要使用到Union如下:

from typing import Uniondef square(number: Union[int, float]):return number ** 2

表示,可以接收int或者float类型的参数,在Python3.10中,我们可以使用|操作符号来简化该操作,如下:

def square(number: int|float):return number ** 2

这个新语法也可以作为instance()issublcass()的第二个参数:

>>> isinstance(1, int | str)
True

更多语法变更:https://docs.python.org/3/whatsnew/3.10.html

http://www.jmfq.cn/news/4930453.html

相关文章:

  • 变色龙app制作平台/站内seo内容优化包括
  • 电子商务网站规书/疫情最新消息今天公布
  • 网站开发公司怎么能接到单子/网络营销策略有哪几种
  • 肇庆 网站建设 域联/推广赚钱一个2元
  • 邢台做移动网站价格表/找百度
  • 做网站多少宽带够/百度热搜榜
  • 19年做网站还能赚钱/软文营销常用的方式
  • 开发和发布网站的主要流程/企业建站系统模板
  • 百度手机模板网站/360收录入口
  • 广州企业网站设计方案/网络营销有哪些推广方法
  • 什么是网页和网站/百度店铺免费入驻
  • 珠海市网站开发公司电话/网站优化的主要内容
  • 网站上的用户注册怎么做的/制作电商网站
  • 网站建设组织/网站seo检测
  • 专做实习生招聘的网站/网站为什么要seo?
  • 怎样与知名网站做友情链接/百度快速排名化
  • 电子商务网站推广实训心得/整站排名优化公司
  • 淳安县建设局网站/怎样在百度上发布免费广告
  • 百度推广代理商名单/全网优化哪家好
  • 十里堡网站建设/seo软文推广
  • 沈阳app开发公司哪家好/河南网站优化公司哪家好
  • 网站开展营销的思路和方法/项目推广方案
  • 网站开发流程注意事项/佛山seo代理计费
  • flash做的小动画视频网站/友情链接你会回来感谢我
  • 网站开发证书要求/视频推广一条多少钱
  • 温州手机网站推广/网站建设公司业务
  • 绿色在线网站模板/品牌营销策划案例ppt
  • 网站建设app开发小程序开发/超链接友情外链查询
  • 网页qq邮箱打不开/河南网站seo靠谱
  • php网站建设制作服务/seo在线培训课程