assert versus print [was Re: The curious behavior of integer objects]

Carl Banks pavlovevidence at gmail.com
Tue Jan 16 16:29:54 EST 2007


Neil Cerutti wrote:
> On 2007-01-16, Ron Adam <rrr at ronadam.com> wrote:
> > I have to admit that part of why assert seems wrong to me is
> > the meaning of the word implies something you shouldn't be able
> > to ignore.  While warnings seem like something that can be
> > disregarded.
>
> Experienced C coders expect assert to behave like that.
>
> The only reason (I know of) to turn off error checking is to
> optimize. However, removing tests won't usually make a big enough
> speed difference to be worth the burthen of testing two different
> versions of the same source code.

I don't know about you, but I tend put very expensive checks in assert
statements (or inside an if __debug__).  Stuff like checking one-to-one
synchronicity between two large trees, checking whether lists are
sorted, etc.  If all you're doing is asserting that x==0, yeah, who
cares.  If you're asserting issorted(list), I think you might want to
shut that off in production code.


> So to me the assert statement is either dubious syntax-sugar or
> dangerous, depending on Python's command line arguments.

If used as intended (i.e., to claim that a correct program should
always meet the condition), it shouldn't be any more dangerous than
omitting the test altogether.

The danger comes from using it to check for things that aren't
indicative of a bug in the program; for instance, to verify input.
Then the program can crash and burn if optimization is turned on.


Carl Banks




More information about the Python-list mailing list