From c0ad3cee4d4819550295f031d9dde0c721377146 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Fri, 1 May 2026 17:01:14 -0500 Subject: [PATCH] Add Assert Is Only A Development Check as a Python TIL --- README.md | 3 +- python/assert-is-only-a-development-check.md | 38 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 python/assert-is-only-a-development-check.md diff --git a/README.md b/README.md index bab0fc3..f1382be 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/). For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter). -_1781 TILs and counting..._ +_1782 TILs and counting..._ See some of the other learning resources I work on: @@ -1050,6 +1050,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Access Instance Variables](python/access-instance-variables.md) - [Access Most Recent Return Value In REPL](python/access-most-recent-return-value-in-repl.md) - [Access Variables Outside Loop Scope](python/access-variables-outside-loop-scope.md) +- [Assert Is Only A Development Check](python/assert-is-only-a-development-check.md) - [Avoid Modification With Frozen Dataclass](python/avoid-modification-with-frozen-dataclass.md) - [Break Debugger On First Line Of Program](python/break-debugger-on-first-line-of-program.md) - [Check If Package Is Installed With Pip](python/check-if-package-is-installed-with-pip.md) diff --git a/python/assert-is-only-a-development-check.md b/python/assert-is-only-a-development-check.md new file mode 100644 index 0000000..359e7b0 --- /dev/null +++ b/python/assert-is-only-a-development-check.md @@ -0,0 +1,38 @@ +# Assert Is Only A Development Check + +The `assert` keyword is used in Python to write a statement that will check some +assertion and raise an error if it isn't met. This is only meant to be used as a +check during development because it can be easily optimized out of the code. + +```python +stuff = None + +assert stuff, "We need to have some stuff to proceed" + +print(f"We have {stuff or 'something'}!") +``` + +If I execute this code with `python`, it will raise on that second line of code. + +```bash +❯ python assert_example.py +Traceback (most recent call last): + File "/Users/lastword/dev/jbranchaud/py-vmt/assert_example.py", line 3, in + assert stuff, "We need to have some stuff to proceed" + ^^^^^ +AssertionError: We need to have some stuff to proceed +``` + +This `assert` statement will be stripped out of the compiled bytecode if the +`-O` (capital o) flag is used. Notice how running the same file with that flag +does not lead to an `AssertionError`. + +```python +❯ python -O assert_example.py +We have something! +``` + +If I want to make sanity checks for situations that would be caused by a bug in +the code, an `assert` statement can be a good candidate. However, if I am making +runtime checks like validating user input, then an `if` statement and raising +something like a `ValueError` is better.