On 12/03/2021 4:31 pm, Chris Angelico wrote:
On Fri, Mar 12, 2021 at 3:53 PM Cameron Simpson <c...@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.

Mike


ChrisA


--
Signed email is an absolute defence against phishing. This email has
been signed with my private key. If you import my public key you can
automatically decrypt my signature and be sure it came from me. Just
ask and I'll send it to you. Your email software can handle signing.

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to