[Python-Dev] (#19562) Asserts in Python stdlib code (datetime.py)

Pascal Chambon chambon.pascal at wanadoo.fr
Sun Nov 17 14:02:37 CET 2013

Le 17/11/2013 12:27, Steven D'Aprano a écrit :
>> What I would like to know is if people *knowingly* add costly asserts
>> to performance-critical code, with the intent of disabling them at
>> runtime using -OO.
> Yes, I have knowingly added costly asserts to code with the intend of
> disabling them at runtime. Was it *performance-critical* code? I don't
> know, that was the point of my earlier rambling -- I could demonstrate a
> speedup of the individual functions in benchmarks, but nobody spent the
> effort to determine which functions were performance critical.


my 2 cents:
asserts have been of a great help in the robustness of our provisioning 
framework, these are like tests embedded in code, to *consistently* 
check what would be VERY hard to test from the outside, from unit-tests.

It makes us gain much time when we develop, because asserts (often used 
for "method contract checking") immediately break stuffs if we make dumb 
programming errors, like giving the wrong type of variable as parameter 
etc. (if you send a string instead of a list of strings to a method, it 
could take a while before the errors gets noticed, since their behaviour 
is quite close)

We also add asserts with very expensive operations (like fully checking 
the proper synchronization of our DBs with the mockups of remote 
partners, after each provisioning command treated), so that we don't 
need to call something like that after every line of unit-test we write.

In production, we then make sure we use -O flag to avoid doubling our 
treatments times and traffic.


More information about the Python-Dev mailing list