Assertion vs. try-except

Nicolas Decoster Nicolas.Decoster at Noveltis.fr
Fri Oct 20 10:16:39 EDT 2000


Thomas Weholt wrote:
> 
> Hi,
> 
> Just wondering what the benefits of assert is, compared to doing a
> try-except for instance. The documentation says :
> "Assert statements are a convenient way to insert debugging assertions
> into a program"

The goal of assert is to say: "you can be sure my function gives this
result (post-conditions) as long as you give this inputs
(pre-conditions), otherwise I can't predict anything and the result is
uncertain". In fact assert allow you to check these conditions while in
testing/debugging phase.

When you use exception you say: "you can do anything you want with my
function, you can be sure I will tell you if something is going wrong".

Most of the time the second solution is development-time expensive. And
with assert you give the "test" responsability to the caller of your
function. Of course this caller can give this responsability to its
caller, and so on. So the test (using exception, for example) is done at
the highest level, saving test at each step.

Nicolas.

--
Tél. : 00 (33) 5 62 88 11 16
Fax  : 00 (33) 5 62 88 11 12
Nicolas.Decoster at Noveltis.fr

Noveltis
Parc Technologique du Canal
2, avenue de l'Europe
31520 Ramonville Saint Agne - France



More information about the Python-list mailing list