[Python-ideas] Pre-conditions and post-conditions

Ethan Furman ethan at stoneleaf.us
Wed Aug 29 23:00:46 EDT 2018


On 08/29/2018 04:53 PM, Hugh Fisher wrote:
> From: Marko Ristin-Kaufmann:

>> There seems to be evidence that design-by-contract is useful. Let me cite
>> Bertrand Meyer from his article "Why not program right?" that I already
>> mentioned before:
>
> I don't think that being useful by itself should be enough. I think new features
> should also be "Pythonic" and I don't see design by contract notation as a
> good fit.

I don't see type annotation notation as a good fit, either, and yet we have it.  Seems to me that DbC would be just as 
useful as type annotations (and I find D'Aprano's example syntax very readable).

> For design by contract, as others have noted Python assert statements
> work fine for simple preconditions and postconditions.

And print statements work fine for simple debugging.  New features are not added for the simple cases, but the complex, 
or non-obviously correct, or the very useful cases.

> Yes there's more to design by contract than simple assertions, but it's not
> just adding syntax. Meyer often uses the special "old" construct in his post
> condition examples, a trivial example being
>
>      ensure count = old.count + 1

I can see where that could get expensive quickly.

> How do we do that in Python? And another part of design by contract (at
> least according to Meyer) is that it's not enough to just raise an exception,
> but there must be a guarantee that it is handled and the post conditions
> and/or invariants restored.

Well, we don't have to have exactly the same kind of DbC as Eiffel does.

--
~Ethan~


More information about the Python-ideas mailing list