[Python-Dev] Re: Introduction

Paul Moore gustav@morpheus.demon.co.uk
Sun, 01 Jun 2003 17:17:10 +0100


Terence Way <terry@wayforward.net> writes:

> On Wednesday, May 28, 2003, at 04:55  PM, Peter Jones wrote:
>
>> That being said, you still haven't explained *why* contracts belong
>> in docstrings (or in documentation in general). They are executable
>> code; why not treat them as such?
>>
> Okay, the whole docstring vs syntax thing, and I'm going to quote
> liberally from Bertrand Meyer's Object Oriented Software
> Construction, 1st edition, 7.9 Using Assertions.

Note: I'm *not* a fan of Eiffel-style formal pre and post conditions. 
I probably wouldn't use them, personally. But as I could end up
dealing with others' code which uses such a feature if it was added, I
do have an interest. Also, I haven't read the PEP yet (I'm offline
right now).

But I disagree fairly strongly with the idea of having pre/post
conditions in docstrings. That is *not* what docstrings are for.
Docstrings are documentation, not arbitrary metadata.

How about this as a proposal? Attach pre and post conditions to a
functions as function attributes. A silly example:


    def f():
        pass

    def pre():
        assert True
    f.pre = pre

    def post():
        assert True
    f.post = post

This is a bit wordy, but entirely doable with Python as it stands and
the conditions are syntax checked at definition time, just like they
should be. If pre/post conditions turn out to be so useful that they
deserve a more concise syntax, *that's* the time to propose better
syntax. (One obvious possibility would be to allow more flexibility in
def statements, so that

    def f.post():
        whatever

becomes possible.)

Paul.
-- 
This signature intentionally left blank