Why assert is not a function?

Chris Angelico rosuav at gmail.com
Fri Mar 12 01:20:19 EST 2021


On Fri, Mar 12, 2021 at 5:03 PM Mike Dewhirst <miked at dewhirst.com.au> wrote:
>
> On 12/03/2021 4:31 pm, Chris Angelico wrote:
> > 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 :)
>
> I haven't been following this thread so please forgive me if this has
> been said ...
>
> My understanding of the reason for assert is to support the "design by
> contract" programming style of Eiffel as espoused by Bertrand Meyer. I
> don't suppose it makes much difference whether it is a function or a
> callable for that - and when I first saw it I was appropriately confused
> - but I only ever used it when I was absolutely certain the assertion
> was bullet-proof. And it is a long time since I did that. I prefer
> try-except nowadays.
>

Definitely use something other than assertions for that.

ChrisA


More information about the Python-list mailing list