assert¶
Learning objectives
- Understand design by contract
- Understand algorithm names increase expressiveness of code
- Understand difference between
if
andassert
in function writing
The role of assert
within functions¶
Within functions, assert
is used for:
- as a stub
- to do things in debug mode only
- to document assumptions a developer makes
assert
differs between debug and release¶
$ cat assert.py
assert 1 == 2
$ python -O assert.py
$ python assert.py
Traceback (most recent call last):
File "/home/richel/assert.py", line 1, in <module>
assert 1 == 2
AssertionError
assert
as a stub¶
assert
in debug mode¶
def align_two_dna_sequences(dna_sequences):
"""Internal function to align two DNA sequences"""
assert len(dna_sequences) == 2 # TODO
Superior to documentation, as it cannot be ignored.
'Assert liberally to document internal assumptions and invariants' @sutter2004cpp chapter 68.