raise Assertionerror vs. assert python

僤鯓⒐⒋嵵緔 提交于 2020-07-03 06:19:51

问题


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 ?

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 ?

Thanks a lot already


回答1:


Those two code examples are equivalent, with the exception that assert statements can be globally disabled with the -O command-line flag.



来源:https://stackoverflow.com/questions/48408692/raise-assertionerror-vs-assert-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!