Skip to content

Commit

Permalink
Merge pull request #41 from nipraxis/enh/assert-vs-raise
Browse files Browse the repository at this point in the history
MRG: Add discussion of when to use assertions vs exceptions
  • Loading branch information
matthew-brett authored Aug 28, 2023
2 parents dc3d4e2 + 9e13fb0 commit 8f500de
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions assert.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,23 @@ assert 0
# Error
assert None
```

<!-- #md -->

```{warning}
Note that `assert` *should not* be used in order to raise errors at runtime.
The use cases for assertions are development, testing and debugging your code,
and are for declaring "This condition is expected to be to true".
If this always succeeds during testing, it is considered safe to remove
[in production](https://en.wikipedia.org/wiki/Deployment_environment#Production).
If Python is run [with optimization](https://docs.python.org/3/using/cmdline.html#cmdoption-O),
`assert` statements will be removed for speed, and you as the developer do not
control whether your code is run with optimization or not.
For error conditions that you expect to encounter in runtime,
[raise exceptions](https://docs.python.org/3/tutorial/errors.html#raising-exceptions).
```

<!-- #endmd -->

0 comments on commit 8f500de

Please sign in to comment.