mirror of
https://github.com/jbranchaud/til
synced 2026-07-03 16:18:24 +00:00
Add Assert Is Only A Development Check as a Python TIL
This commit is contained in:
@@ -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).
|
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:
|
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 Instance Variables](python/access-instance-variables.md)
|
||||||
- [Access Most Recent Return Value In REPL](python/access-most-recent-return-value-in-repl.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)
|
- [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)
|
- [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)
|
- [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)
|
- [Check If Package Is Installed With Pip](python/check-if-package-is-installed-with-pip.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 <module>
|
||||||
|
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.
|
||||||
Reference in New Issue
Block a user