Design by Contract for Python

Carl Banks imbosol at aerojockey.com
Thu May 15 14:54:39 EDT 2003


Terence Way wrote:
> Thanks for the info.
> 
> > Metaclasses offer a neat, cool way of grouping related functions
> > like preconditions and posconditions...
> 
> Meta-classes are, in fact, the technique used by Daniel Arbuckle
> in his excellent PyDBC package, see
>     http://savannah.nongnu.org/projects/pydbc/
> Although I like the "grouping" that you propose.  But I'm not sure it
> would work with inheritance ("virtual" functions).

It absolutely will.  The metaclass definition returns an ordinary
function.


> Or variable arguments.

Yeah, I presented this as a proof-of-concept, not as the one-and-only
way to possibly do it.  That my example doesn't work for variable
arguments doesn't mean it's manifestly impossible.


> I wish instance methods could have their own dict,
> just like static functions.  Then you could do:
>     class foo:
>          def func(self, a, b):
>                ...
>          func.pre = ...
>          func.post = ...

Granted.


[snip]
> Conclusion: either the pre-condition function returns a tuple of its
> result (valid, old-values) OR a separate function must be written to
> save old values.

Thank you for the explanation of why a save function would be needed.
Again, just because my example didn't do it, doesn't mean these issues
can't be handled by well-thought out code.


> I'm beginning to think that the complexities of DBC are not in
> "installing" the condition check functions, but in coming up with a
> system that:
> a) can be retrofitted into existing code;
> b) matches Eiffel's well-thought-out semantics; and
> c) doesn't require a lot of programmer effort.
> 
> Thanks again.


-- 
CARL BANKS




More information about the Python-list mailing list