I love assert

Marko Rauhamaa marko at pacujo.net
Wed Nov 12 18:12:44 EST 2014


Ian Kelly <ian.g.kelly at gmail.com>:

> On Wed, Nov 12, 2014 at 3:47 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
>> Asserts have nothing to do with them being optimized out. Asserts are
>> communication.
>>
>> Apart from idiomatic style, there is no difference between
>>
>>     # never reached
>>
>>     assert False
>>
>>     raise RuntimeError('Unreachable code reached')
>
> If the purpose is communication, then the comment is most effective,
> as it can easily convey anything you want. If the purpose is to detect
> programming errors, then the RuntimeError is most effective, as it
> will always raise an error in the event it is reached.
>
> assert False is a strange hybrid of the two that is less effective at
> both purposes.

If assert weren't there, I would likely choose one of those two
alternatives. But assert is there so there's no reason to avoid it.


Marko



More information about the Python-list mailing list