I love assert

Ian Kelly ian.g.kelly at gmail.com
Wed Nov 12 17:07:19 EST 2014


On Wed, Nov 12, 2014 at 3:04 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Wed, Nov 12, 2014 at 2:56 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
>> How would it be better if you removed the assert then?
>
> You don't need to remove it. Just reorganize it to make sure it
> indicates actual exhaustion of possibilities. E.g. using the "assert
> False" pattern from your post:
>
> if status == OK:
>     ...
> elif status == ERROR:
>     ...
> elif status == WARNING:
>     ...
> else:
>     assert False

Although to be honest I'd rather use something like "raise
RuntimeError('Unreachable code reached')" than "assert False" here. If
the expectation is that the code will never be executed, then there's
no reason to ever optimize it out.



More information about the Python-list mailing list