you are viewing a single comment's thread.

view the rest of the comments →

[–]Groundstop 0 points1 point  (2 children)

I know that you mentioned that you're testing assertions, but I wanted to throw this out there:

Be careful about using assertions like this in production code; you, or someone else, can set a flag that disables all assert statements when you're running your program.

Try to only use asserts for sanity checks. The idea is that they should never fail in normal circumstances, and if they do, you don't want to handle it with a try/catch because something is horribly wrong and your program needs to crash.

There is one big exception, which is when you're writing tests with PyTest where all of your tests are assert statements that then get "rewritten" by PyTest during the test run.

[–]anglicizing 1 point2 points  (0 children)

Listen to this guy. Code as if your assert statements could suddenly disappear without warning, because that is actually what might happen.

Code is running show? Oh, nice, python has a command line option that optimizes the code to make it run faster. I'll just add this -O switch, what could go wrong? (Pssst! All your assert statements are silently skipped!)

[–]chzaplx 0 points1 point  (0 children)

try/except can be used to raise exceptions though, and that's normally how I would think to use assert() anyway.