Why assert is not a function?

Chris Angelico rosuav at gmail.com
Fri Mar 12 00:31:09 EST 2021


On Fri, Mar 12, 2021 at 3:53 PM Cameron Simpson <cs at cskk.id.au> wrote:
> For me, try/except is for when something might reasonably "go wrong" in
> normal use, even niche normal use. Whereas assert is for things which
> should _never_ occur. Roughly, again for me, try/except if for catching
> misuse and assert is for catching misdesign/misimplementation.

Something like that, yeah. An assertion failure represents a bug *in
this code*, something that shouldn't ever happen. If it's possible to
trigger the failure with some other piece of code (calling something
with bad arguments, or whatever), then assert is the wrong tool for
the job. Similarly, if you find yourself catching AssertionError
anywhere outside of unit testing, something is horribly wrong
somewhere :)

ChrisA


More information about the Python-list mailing list