[Learning] The dangers of assert in Python (a.k.a know what you're doing!)
There are many ways to find bugs in Python code: the built-in debugger (pdb), a healthy amount of unit tests, a debugger in an IDE like Pycharm or Visual Studio, try/catch
statements, if/else
statements, assert statements
, or the tried and true practice of covering every inch of your codebase in print()
statements like it’s going out of style.
Assert statements can help us catch bugs quickly and are far less intrusive than copious amounts of print
statements. However, unlike print
statements, assert statements can be unexpectedly risky to use!
This article explores how to use asserts safely and what causes them to be unsafe. By the end of this article, you’ll know how to use assert
most optimally without inadvertently opening yourself up to security issues.