问题
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