Python: Assertions

Execution of assert‘s is controlled by __debug__ variable. In optimized mode (-o or -oo), __debug__ is False, so assert‘s are no-op.

Best practice:

  • DON’T use assert for code that must be executed for program correctness like checking user inputs.
  • DO use assert on invariants.
  • DO use assert to check inputs coming from other parts of a program.

Leave a comment