[Tutor] about assert

alan.gauld at freenet.co.uk alan.gauld at freenet.co.uk
Wed Sep 13 20:11:10 CEST 2006


>>Python manual has a very brief introduction 
>>of "assert" statements. It is very difficult 

First you probably don't need to use asserts 
very often, they are useful if you are building 
production strength code but for most users 
of Python the exception mechanism is good enough.

Basically we use asserts to check pre and post 
conditions as well as\ invariants in functions.
Anything other than that should be done using 
explicit if/else or exceptions. The reason for 
that is that asserts only operate when the code 
is in debug mode... so they should not be 
used for normal run time error detection.

Typical things to check are that input values 
are within expected ranges and of desired types.
Also that the final retrurn value of a function 
is within expected limits.
Once we finish testing the overhead of those 
checks might not be required in the final 
version if we are sure they will never 
deviate from the test results.

The syntax can be demonstrated quite easily 
and Luke has done that, the intent is a little 
less obvious and IMHO really needs a larger 
example to make the value obvious. Since I'm 
at a cyber-cafe net terminal I dont feel like 
trying that just now! :-)

HTH,

Alan G.



More information about the Tutor mailing list