I love assert

Marko Rauhamaa marko at pacujo.net
Wed Nov 12 23:47:31 CET 2014

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

> 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.

Asserts have nothing to do with them being optimized out. Asserts are

Apart from idiomatic style, there is no difference between

    # never reached

    assert False

    raise RuntimeError('Unreachable code reached')

    1 / 0

    print("Hello world")

since, after all, that line is never reached!

Out of these variants, I find

    assert False

to be the most tasteful. It is concise, to the point and provided by the
core language. If it didn't exist, I'd have to resort to one of the
other alternatives.


More information about the Python-list mailing list