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

Guido van Rossum guido at python.org
Thu Jan 18 23:00:01 EST 2018

On Thu, Jan 18, 2018 at 7:51 PM, Nikolas Vanderhoof <
nikolasrvanderhoof at gmail.com> wrote:

> I sometimes wish that Python included a richer set of assertions rather
>> than just a single `assert` keyword. Something like Eiffel's concept of
>> pre-conditions, post-conditions and invariants, where each can be
>> enabled or disabled independently.
> Has something like this been proposed for Python before?
> This seems to align more with the intended use of assert that's been
> pointed out in this thread.
> In what case though would one want to disable some but not all of these
> pre, post, or invariant assertions?

Oh, many times, starting in the late '90s IIRC (Paul Dubois was a big fan).

The problems are twofold: (a) it would require a lot of new keywords or
ugly syntax; and (b) there would have to be a way to enable each form
separately *per module or package*. Eiffel solves that (AFAIC) through
separate compilation -- e.g. a stable version of a library might disable
invariants and post-conditions but keep pre-conditions, since those could
be violated by less mature application code; or a mature application could
disable all checks and link with optimized library binaries that also have
disabled all checks. I'm sure other scenarios are also viable.

But that solution isn't available in Python, where command line flags apply
to *all* modules being imported.

(Note: even if you have a solution for (b), getting past (a) isn't so easy.
So don't get nerd-sniped by the solution for (b) alone.)

--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180118/77c1f04b/attachment.html>

More information about the Python-ideas mailing list