[Python-ideas] Specify an alternative exception for "assert"

Random832 random832 at fastmail.com
Mon May 2 11:36:07 EDT 2016

On Mon, May 2, 2016, at 11:28, Giampaolo Rodola' wrote:
> Why such a constraint? I think most of the times the desired exception
> would be either ValueError or TypeError, both of which do not inherit
> from
> AssertionError.

The point is that it's not appropriate to use assert for checks that are
part of the semantics of the function.

If all that you gain is having the check logic on the same source line
as the raise for the stack trace, why not simply do "if value < 1: raise
ValueError('value must be >= 1')"?

if/raise is only two more characters than assert. "if not/raise" is a
*bit* more, but not enough to worry about. You could trim off a
character (and have a better style if we're squeamish about single-line
suites) by adding a "raise ... if ..." syntax.

More information about the Python-ideas mailing list