usage of assert and -O

Carl Banks imbosol-1049850353 at aerojockey.com
Tue Apr 8 22:14:59 EDT 2003


Matthew Russell wrote:
> I would be interested to know what others think about usage of
> assert in context with optimised python code, in particular in
> releation to unittesting, as this is where i think it would be
> useful - if not critical to have one way of doing it (i.e not useing
> assert or -O [ one or the other ])



Well, theoretically, the point of assert is to test for conditions
that are supposed to be impossible.  (For example, suppose an argument
to a function must be positive.  Your program never calls it with a
negative value; therefore, it's impossible for the the argument to be
negative.)

The idea is that, since the conditions are impossible, you don't need
to waste time checking for them in production code.  However, when
debugging, you would like these checks so that you don't accidentally
do something that's supposed to be impossible.  You might think that
function never gets called with a negative value, but what if you
overlook something and suddenly it does get called with a negative
value?  Then your assertion catches it.

A lot of people think it's just a questionable optimization.  Just
checking if a value's positive isn't too expensive, and certainly not
worth removing in production code.

But I don't think it's that simple.  Sometimes you want to do a
not-so-cheap assertion (say, that a list is sorted).  In development,
you might want to make sure a list passed to a binary search function
is sorted, but you absolutely don't want to have this check enabled
production code, because checking that a list is sorted is O(n),
whereas a binary search is O(log n).  In this case, eliminating the
assertion test in production code is more than an optimization.

This is why I think assertions are useful in general.  I use them
frequently in C.

However, I don't use them as much in Python for a couple reasons.
First, Python catches a lot more errors than C, so it is not as
crucial to test for errors in Python.  Second, Python is an
interpreter, and it tests assertions by default.

This, IMO, is a mistake.  Python should only test assertions when a
debugging flag is present.  The reason is, I can't trust a user to
always run the optimized version.  Thus, I can't make expensive
assertions like ensuring a list is already sorted.  Yet this is when
assertions are most important.  Second, because assertions are tested
by default, a lot of people are tempted to use them in lieu of regular
error checking.


As for unit testing, I agree with Peter Hanson: it's a whole 'nother
realm of error testing.  I'm not an expert in unit testing, but it
seems to me unit testing serves a very different purpose than
assertions.  Unit tests try to enforce the robustness of a function by
throwing every possible sort of object at it, to make sure it fails
properly.  Assertions guard against unintended use of a function: they
make sure every possible sort of object doesn't get thrown at it.



-- 
CARL BANKS




More information about the Python-list mailing list