Contracts for Python

alex23 wuwei23 at gmail.com
Tue Oct 28 21:48:36 EDT 2008


On Oct 29, 3:47 am, "Paulo J. Matos" <pocma... at gmail.com> wrote:
> I am wondering if there is any work on contracts for Python. I could
> only find PEP316, however, I am wondering if there is any official
> support for it already (tools I mean), and if it is or if it will be
> officially supported in any of the next releases of Python.

It's possible to get a simplistic design-by-contract approach without
external libs by using 'assert'.

Here's a modified example from PEP 316:

class circbuf:

    def __init__(self, leng):
        """Construct an empty circular buffer."""

        # pre
        assert leng > 0, "pre: length not positive"

        ...

        # post
        assert self.is_empty(), "post: buffer not empty"
        assert len(self.buf) == leng, "post: buffer length incorrect"



More information about the Python-list mailing list