LWN.net Logo

Contracts for Python

Contracts for Python is a Python implementation of a concept that comes from the Eiffel language, Design by Contract. Programming by Contract allows a programmer to document a function/class with statements describing behavior." Along with the regular code, contracts for functions and methods add pre and post condition checking, and code for testing correct operation of the code.

A draft Python language PEP (Python Enhancement Proposal) for Design by Contract has been submitted. The status of PEP 316 is currently Deferred.

Programming contracts extends the language to include invariant expressions for classes and modules, and pre- and post-condition expressions for functions and methods. These expressions (contracts) are similar to assertions: they must be true or the program is stopped, and run-time checking of the contracts is typically only enabled while debugging. Contracts are higher-level than straight assertions and are typically included in documentation.

The PEP states that compared to Assertions, Contracts produce better documentation, and make for easier testing.

The Eiffel Design by Contract document goes into greater detail on the subject:

To be sure that our object-oriented software will perform properly, we need a systematic approach to specifying and implementing object-oriented software elements and their relations in a software system. This article introduces such a method, known as Design by Contract. Under the Design by Contract theory, a software system is viewed as a set of communicating components whose interaction is based on precisely defined specifications of the mutual obligations -- contracts.

Version 1.0 beta 3 of Design by Contract just came out. The CHANGELOG file details the recent changes, which include support for Python 2.2 and Jython among other things.


(Log in to post comments)

Copyright © 2003, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds