引发AssertionError与断言python [英] raise Assertionerror vs. assert python
问题描述
引发Assertionerror和断言以建立故障"之间的最大区别是什么.对代码有什么影响?并且在某种程度上,另一个或另一个是pythonic吗?
What are the big differences between raise Assertionerror and assert to build in a "fault". What are the effects on the code ? and is one or the other more pythonic in a way ?
我提出这个问题的原因是因为我正在学习编程,现在我们进行了练习,例如,当x!="ok"(x是某个数字)时,我们需要获取AssertionError"false".
The reason for my question is because I am learning to program right now we have exercise where for example when x != "ok" (x be a certain number) we need to get an AssertionError "false".
我在网上查找了此内容,然后他们说要设置以下代码:
I looked this up online and then they say to set the following code:
if x != 0:
raise AssertionError ("false")
但是我的老师也经常使用以下内容
but my teachers also use the following a lot
assert x == "ok","fout"
此方法的(缺点)有哪些优点?
What are the (dis)advantages to this approach ?
已经非常感谢
推荐答案
这两个代码示例是等效的,但可以使用-O
命令行标志全局禁用assert
语句.
Those two code examples are equivalent, with the exception that assert
statements can be globally disabled with the -O
command-line flag.
这篇关于引发AssertionError与断言python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!