[Python-Dev] the role of assert in the standard library ?

Barry Warsaw barry at python.org
Thu Apr 28 16:37:33 CEST 2011


On Apr 28, 2011, at 04:34 AM, Terry Reedy wrote:

>On 4/28/2011 3:54 AM, Tarek Ziadé wrote:
>> Hello
>>
>> I removed some assert calls in distutils some time ago because the
>> package was not behaving correctly when people were using Python with
>> the --optimize flag. In other words, assert became a full part of the
>> code logic and removing them via -O was changing the behavior.
>>
>> In my opinion assert should be avoided completely anywhere else than
>> in the tests. If this is a wrong statement, please let me know why :)

>My understanding is that assert can be used in production code but only to
>catch logic errors by testing supposed invariants or postconditions. It
>should not be used to test usage errors, including preconditions. In other
>words, assert presence or absence should not affect behavior unless the code
>has a bug.

I would agree.  Use asserts for "this can't possibly happen <wink>"
conditions.

-Barry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-dev/attachments/20110428/58c85c34/attachment.pgp>


More information about the Python-Dev mailing list