[Python-Dev] Re: Introduction

Barry Warsaw barry@python.org
Sun, 1 Jun 2003 18:38:54 -0400


On Sunday, June 1, 2003, at 12:17 PM, Paul Moore wrote:
> 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.)

Although I haven't played with it much, in the back of my mind, I think 
descriptors might be the right implementation technique for this.  And 
the syntax that seems to keep coming up is something like:

def f(x, y) [pre, post]:
   foo()

For other reasons, I'm finding that I'd really like to see this syntax 
for function decorators embodied in its own PEP.

-Barry