ANN: Design By Contract for Python 1.0 beta 1

Jeff Epler jepler at unpythonic.net
Mon May 26 12:56:07 EDT 2003


On Thu, May 22, 2003 at 10:04:18PM -0400, Terence Way wrote:
> On Thu, 22 May 2003 20:14:25 -0400, Cristian Tibirna wrote:
> 
> > Thus, question. Is this (deactivation) a planned feature for your
> > wonderful tool?
>  
> Thanks for your feedback.
> 
> You bet!  Actually, you need to specifically *enable* contract
> checking, by calling contract.checkmod on a module.  If you don't
> do this, then the contracts just stay in the docstrings, no extra
> overhead at all.  Contract checking is disabled anyway when running
> in optimized mode (-O on the python command line).
> 
> What I don't have (yet) is some way to have only pre-conditions
> checked -- Eiffel offers this and it makes a lot of sense.

Of course, even this may radically slow your program.  For instance, sort()
is presumably O(n lg n) runtime complexity, but the precondition that tests
that each pair of items is comparable is O(n^2) complexity.

Is there a reason that you don't use exception subclasses to differentiate
between different kinds of contract failures?  The hierarchy might look
like
	ContractViolationError
		PreconditionViolationError
		PostconditionViolationError
		ClassInvariantViolationError

I glanced at the Eiffel page you linked form the PEP and they had some form
of error recovery capability.  Did you address why you decided not to
include this in your PEP?

If your feature could be adopted either in the form of a
specially-formatted docstring or as actual "code", which would you prefer?
Typically, editors will decline to do syntax highlighting or automatic
indentation of string contents.

Why do you use "inv:" instead of "invariant:"?  The only exceptions to the
"english word" rule in Python's existing reserved words are "def", "del" and
"elif". (Of course, 3 exceptions out of 29 is quite a few)  For whatever
reason, I don't feel as weird about "pre:" and "post:" as I do about "inv:".

Does Eiffel have a syntax for *loop* invariants? eg
	def index_of_largest(l):
		largest = l[0]
		for i in range(1, len(l)):
			invariant: forall(range(i), lambda x: largest >= l[x])
			if l[i] > largest: largest = l[i]
				
Jeff





More information about the Python-list mailing list