How can I make my assertions smarter?

William Tanksley wtanksle at hawking.armored.net
Wed Nov 3 14:27:51 CET 1999


On Mon, 01 Nov 1999 21:50:47 +1100, Stuart Hungerford wrote:
>Hi all,

>I'm using some simple functions to implement crude precondition,
>postcondition and invariant checking in Python classes.

Cool.  Unfortunately, to get anything like Eiffel, you'd have to modify
the language (add a keyword), because such things require a statement, not
an expression.  That's not impossible, although it might be more work than
you're willing to do, and it would certainly not become part of Python 1.x
(to say the least).

>Is there some neat way to wrap these tests up so I can print any
>code expression that fails, e.g:

>     Precondition failed foo.py line 42: spoon.shape == 'bent'
>which would appear at line 42 of foo.py as
>     pre(spoon.shape == 'bent')

assert spoon, "There is no spoon"

>I can see how to use environment variables or global variables to
>selectively enable precondtions etc, although any hints for doing that
>would be appreciated too.

Preconditions can also be implemented via aspects.  Aspect Oriented
Programming is _really_ cool, and with the author of JPython helping with
AspectJ I suspect that Python will someday handle them.

Aspects are really cool -- like a semantic preprocessor on steroids.

>Stu

-- 
-William "Billy" Tanksley




More information about the Python-list mailing list