[Tutor] try, except syntax

Steven D'Aprano steve at pearwood.info
Thu Aug 2 19:54:11 EDT 2018


On Thu, Aug 02, 2018 at 12:19:41PM -0700, Marc Tompkins wrote:

> What you want in that case is an assertion:
> 
> try:
>     assert type(uvc)==float
> except AssertionError as e:
>     print(e, msg)
> 
> An assertion says "The following statement is True.  If it isn't, I'm going
> to throw an exception."  They're especially useful when writing tests, and
> should be part of your flow-control toolbox.


Absolutely not! Sorry Marc, but that's *terrible* advice (sorry I don't 
have the delicacy to sugar coat that this morning, I've been railing 
against the misuse of assert for what sometimes seems like a thousand 
years...), it is a misuse of assert and buggy as well.

https://import-that.dreamwidth.org/676.html



-- 
Steve


More information about the Tutor mailing list