栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

提高尝试和断言之间的区别

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

提高尝试和断言之间的区别

断言:

当您要根据特定条件“停止”脚本并返回某些内容以加快调试速度时使用:

list_ = ["a","b","x"]assert "x" in list_, "x is not in the list"print("passed") #>> prints passedlist_ = ["a","b","c"]assert "x" in list_, "x is not in the list"print("passed")#>> Traceback (most recent call last):  File "python", line 2, in <module>AssertionError: x is not in the list

提高:

这很有用的两个原因:

1
/与try和except块一起使用。引发您选择的错误,可以像下面这样自定义,并且如果您

pass
contiune
脚本不停止脚本;或者可以是预定义的错误
raisevalueError()

class Custom_error(baseException):    passtry:    print("hello")    raise Custom_error    print("world")except Custom_error:    print("found it not stopping now")print("im outside")>> hello>> found it not stopping now>> im outside

注意到它没有停止吗?我们可以使用except块中的exit(1)停止它。

2 / Raise也可以用于重新引发当前错误,以将其传递给堆栈,以查看是否有其他方法可以处理它。

except SomeError, e:     if not can_handle(e):          raise     someone_take_care_of_it(e)

试/除块:

完全按照您的想法进行尝试,尝试某些操作,如果出现错误,您可以按照自己的意愿进行处理。由于上面有一个,所以没有例子。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/659522.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号