21

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 an exercise where for example when x != 0 we need to get an AssertionError "false".

I looked this up online, where I found the following code:

if x != 0:
    raise AssertionError ("false")

But my teachers also use the following a lot:

assert x == 0,"fout"

What are the (dis)advantages of each approach?

Thanks a lot in advance.

5
  • Can you elaborate on your question a bit? Generally, assertions are for testing your code and finding bugs. assert statements can be ignored with compiler flags. Commented Jan 23, 2018 at 18:46
  • @juanpa.arrivillaga i explained it a bit more. Hope this helps :) Commented Jan 23, 2018 at 18:53
  • As juanpa explained, the assert statement is generally for unit-testing or finding issues during the development process. This does not mean you will necessarily see the error. On the other hand, raising an AssertionError means that the error will be raised by the code, and that any calling processes can deal with this error as needed. Commented Jan 23, 2018 at 19:01
  • This may be useful as well: stackoverflow.com/questions/40182944/… Commented Jan 23, 2018 at 19:01
  • @Brandon Barney. So if I got this correct you use assert during development but then for the finished program you use Assertionerror. Or not quite ? And can you give an example of not seeing an error ? I don't quite see how that can happen .. Commented Jan 23, 2018 at 19:04

1 Answer 1

26

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

See:

# script.py
assert 0, "statement"
raise AssertionError("error")

Which produces different errors with and without the -O flag:

$ python  script.py 
Traceback (most recent call last):
  File "/tmp/script.py", line 1, in <module>
    assert 0, "statement"
AssertionError: statement
$ python -O script.py 
Traceback (most recent call last):
  File "/tmp/script.py", line 2, in <module>
    raise AssertionError("error")
AssertionError: error
Sign up to request clarification or add additional context in comments.

6 Comments

This command-line flag. Is this something I manually enter? (I have never heard of this before and a first google round hasn't bin helpful)
When you run your code by typing python myscript.py, you can instead type python -O myscript.py. (If you run code from an IDE instead of the command-line, your IDE probably has an option somewhere to achieve the same effect.)
It can also make a difference to pylint.
the assert <statement>, "message" syntax is less hacker secure because it can be disabled. I've been told to use raise AssertionError because it is safer against malicious stuff. I think the Codacy code reviewer tool recommends this.
If you're using assertions to guard against attackers, you're already in the wrong place, IMHO.
Agreed. This may not matter. But linters seem to think there's a better way to do it. Linters are not always checking what matters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.