Assertion idiom in Python

Fredrik Lundh fredrik at effbot.org
Thu Nov 2 11:01:33 EST 2000


Syver Enstad wrote:
> What is the idiomatic way to express an assertion in Python?

use the assert statement:

    assert condition, description
    assert len(L) > 10, "not enough data points"
    assert type(A) is type(""), "requires a string"

> I guess throw an exception, but what kind?

assert throws an AssertionError if the condition is false.

(note that assert statements are removed if you run Python
with the -O option)

</F>





More information about the Python-list mailing list