usage of assert and -O

Erik Max Francis max at alcyone.com
Tue Apr 8 21:46:40 EDT 2003


> Matthew Russell wrote:

> I understand that when you invoke the interpreter with the -O flag
> assert statments are not executed - is this 100% correct? (as i belive
> it to be)

Yes, it is correct.

> and if so, shouldn't the use of assert (if not assert itself) be
> depricated?

No, why should it be?  Traditionally, asserts are used in debug mode
where you're checking invariants, things that are never supposed to
happen, and so on.  You're including them for extra layers of immediate,
in-your-face testing -- if something goes wrong and you're running in
debug mode (in Python, no -O option), you want to know about it
immediately.  The asserts themselves add no additional functionality, so
when you're not running in debug mode you want them totally skipped
over.  (That's why, in C or C++, assert is actually a macro that expands
to nothing in non-debug mode.)

> I note that in unittest.py assert is not use,
> we have failIf or assert_ instead - thus if you avoided using assert
> in your own tests you can saftly run all your unittests optimised....

But that's because unit tests are something different, though obviously
both are related to testing.  Unit tests are standalone tests of a
component to make sure it behaves the way that's expected; asserts are
internal mechanisms used to make sure that invariants stay invariant or
that things that shouldn't happen, don't.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ The most exhausting thing in life is being insincere.
\__/ Anne Morrow Lindbergh
    PyUID / http://www.alcyone.com/pyos/uid/
 A module for generating "unique" IDs in Python.




More information about the Python-list mailing list