Clarity vs. code reuse/generality

Terry Reedy tjreedy at udel.edu
Sat Jul 11 01:04:45 EDT 2009


Steven D'Aprano wrote:
> On Fri, 10 Jul 2009 12:27:25 -0400, Charles Yeomans wrote:
> 
>>> (3) assert is absolutely unsuitable for enforcing pre-conditions and
>>> post-
>>> conditions, unless such conditions are mere "guidelines", because
>>> assert
>>> can be switched off at runtime.
>>
>> Unless, of course, you want to switch off such checking at runtime, as
>> you might when using a design-by-contract approach.
> 
> So is design-by-contract just another way of saying "let's hope the data 
> is valid, because if it's not, we're screwed"?
> 
> Perhaps Python should have a new statement, `assume`, used just like 
> `assert` except it never actually performs the test or raises an error.

The manual says quite clearly

"The simple form, assert expression, is equivalent to
if __debug__:
    if not expression: raise AssertionError"

It should be used when and only when one actually wants the double 
condition check, and not as an abbreviation of the second conditional only.

tjr






More information about the Python-list mailing list