cascading python executions only if return code is 0

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Dec 23 12:05:22 EST 2013


Roy Smith wrote:

> Sigh.  Sometimes I'm not sure which is worse.  The anti-assertion
> zealotry on this list, or the anti-regex zealotry.

I'm not anti-assertions. I love assertions. I wouldn't be surprised if I use
assert more than you do. What I dislike is people misusing assertions.

> And, yes, I know that assertions get turned off with -O (frankly, I
> think that's a design flaw).  We don't run with -O.

Until such time as somebody decides they can speed up your code by 5% by
running with optimizations turned on.

Unfortunately, it's not always clear when an assert is appropriate and when
it isn't. Fundamentally, if an assertion can never fail[1], then there's no
point testing for it:

x = []
assert x == [] and len(x) == 0

So there's always tension between "why am I testing something that can't
fail?" and "but what if it does?", and different people are going to draw
the line in different places. So even if you and I draw the line in
different places, I can acknowledge that you seem to be using asserts the
same way I would, as executable comments and for testing program
invariants, and I'm sorry that my rant was aimed in your direction
unnecessarily.





[1] Short of hardware failure or serious bugs in the compiler, but in both
those cases you can't trust the assertions to trigger correctly either.

-- 
Steven




More information about the Python-list mailing list