Simon Willison’s Weblog

Programming by Contract in Python

Programming by Contract seems to be a sister technique to unit testing—instead of (or as well as) writing a set of tests for a piece of code you write a set of pre- and post-conditions for the data being processed which are then checked whenever the program is run in debugging mode. Contracts for Python discusses the technique, providing a reference implementation and a PEP suggesting inclusion of support for the technique in the core language. Programming by contract was first demonstrated in the Eiffel language, and there’s a good introduction to it in as well.

This is Programming by Contract in Python by Simon Willison, posted on 23rd May 2003.

Next: Even more buttons

Previous: Quick tip: Styling blockquotes with CSS

Previously hosted at