How can I make my assertions smarter?
tim_one at email.msn.com
Wed Nov 3 06:41:17 CET 1999
[Stuart Hungerford, desperately hoping that design by contract will
be easier in Python if he calls it by some other name ...]
> I'm using some simple functions to implement crude precondition,
> postcondition and invariant checking in Python classes.
> (Tim: please be gentle ;-)
I am always gentle with you, Stuart! Your heart is in the right place --
although it may as well be firmly stuck up your ass for all the good it's
doing you <wink>.
> 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')
As Michael Hudson amply demonstrated, you're fighting the language. If you
can't live with
assert spoon.shape == 'bent', "precondition failed"
then, if I were you, I'd write a little preprocessor to transform your
pre/post/invariant(...) thingies into the desired code. That's fighting the
language too, of course, but in a way that's easier to extend along with
your ambitions. DBC runs deep! I don't think it can be tacked on
generously-leaving-eiffel-a-reason-to-exist<wink>-ly y'rs - tim
More information about the Python-list