Assertions are bad, m'kay?

Irmen de Jong irmen.NOSPAM at xs4all.nl
Fri Mar 7 19:26:05 EST 2014


On 8-3-2014 1:15, Dan Stromberg wrote:
> On Fri, Mar 7, 2014 at 3:11 AM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
> 
>>
>> Assertions are not bad! They're just misunderstood and abused.
> 
>> You should read this guy's blog post on when to use assert:
>>
>> http://import-that.dreamwidth.org/676.html
> 
> Nice article.
> 
> BTW, what about:
> 
> if value >= 3:
>    raise AssertionError('value must be >= 3')
> 
> ?

I don't think this qualifies as an assertion. Also, because AssertionError is documented
as "Raised when an assert statement fails", I would never use it myself explicitly like
this.

You should use ValueError instead (or a more precise exception such as IndexError, if
appropriate).

Irmen







More information about the Python-list mailing list