Assertions are bad, m'kay?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Mar 7 06:11:49 EST 2014


On Fri, 07 Mar 2014 18:16:55 +1100, Chris Angelico wrote:

> They produce the wrong exception type, they disappear when you least
> expect them, and now we have another reason not to use assert.
> 
> http://xkcd.com/1339/
> 
> Abusing assert for arg checking violates XKCD 1339. Write
> standards-compliant code!

Assertions are not bad! They're just misunderstood and abused.

(By the way, assertions are not the same as assumptions. Asserts can be 
used to check that assumptions are correct, or to check the internal 
logic of your reasoning. Whereas assumptions are just accepted as if they 
were correct, no questions asked.


You should read this guy's blog post on when to use assert:

http://import-that.dreamwidth.org/676.html

It's pretty damn good, if I do say so myself...

*whistles innocently*


-- 
Steven D'Aprano
http://import-that.dreamwidth.org/



More information about the Python-list mailing list