Anything equivalent to cassert in C++?

Jonathan Hartley tartley at
Fri Nov 27 15:15:28 CET 2009

On Nov 26, 6:08 pm, Jean-Michel Pichavant <jeanmic... at>
> Peng Yu wrote:
> > There are some assertion code (testing if a condition is false, if it
> > is false, raise an Error object) in my python, which is useful when I
> > test my package.  But such case would never occur when in the produce
> > code. If I keep them in if statement, it will take some runtime. I'm
> > wondering what is the practice that take care of the assertion code in
> > python.
> Would never occur ? You can't use 'would' and 'never' in the same
> sentence. That is the kind of concect that make (to me) asserts diffcult
> to use.
> I personally never use asserts in python, cause I will end up handling
> with asserts what I should in fact handle properly in what you call the
> production code.
> Anyway that does not answer your question I'm sorry to be off topic,
> 'tis just a thought. Simon gave you the proper answer.
> Jean-Michel

I can see this issue has several facets to it, so I don't intend to
flagratly contradict your experience Jean-Michel, but I think that in
at least some circumstances asserts used in this way can be valuable.

One of the interesting points to come out of the recent flurry of
empirical studies of software engineering is that quality of code as
measured by defect density shows a definite negative correllation with
use of asserts in production code. (Although the significance of this
is somewhat lessened by the fact that both use of asserts and low
defect density are correlated with the level of developer experience.)

Original Microsoft paper here:
Summary article discussing that paper and others:

Personally, I'm curious as to whether the benefit from asserts used in
this way is in helping the developer to clarify in their own mind
exactly what their expectations and invariants are.

More information about the Python-list mailing list