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

Steven D'Aprano steve at pearwood.info
Thu Jan 18 23:51:23 EST 2018


On Thu, Jan 18, 2018 at 08:00:01PM -0800, Guido van Rossum wrote:
> 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.)


Indeed. I fear that Python's design will never be a good match for 
Eiffel's Design By Contract. Nevertheless I still have hope that there 
could be things we can learn from it. After all, DBC is as much a state 
of mind as it is syntax.

Here's a blast from the past:

https://www.python.org/doc/essays/metaclasses/

http://legacy.python.org/doc/essays/metaclasses/Eiffel.py

This was my first introduction to the idea of software contracts!



-- 
Steve


More information about the Python-ideas mailing list