[Python-ideas] Repurpose `assert' into a general-purpose check

Chris Angelico rosuav at gmail.com
Mon Nov 27 21:19:12 EST 2017

On Tue, Nov 28, 2017 at 1:12 PM, Ivan Pozdeev via Python-ideas
<python-ideas at python.org> wrote:
> The `assert' statment was created the same as in previous languages like
> C/C++: a check to only do in debug mode, when you can't yet trust your code
> to manage and pass around internal data correctly. Examples are array bounds
> and object state integrity constraints.
> Unlike C, Python does the aforementioned checks all the time, i.e. it's
> effectively always in "debug mode". Furthermore, validation checks are an
> extremily common use case.
> I have been using assert in my in-house code for input validation for a long
> time, the only thing preventing me from doing the same in public code is the
> fact that it only raises AssertionError's while library code is expected to
> raise TypeError or ValueError on invalid input.

Actually, Python does have a way of disabling assertions (the -O
flag), so they should be treated the same way they are in C.
Assertions should not be used as shorthands for "if cond: raise Exc"
in the general case.


More information about the Python-ideas mailing list