Assertions are bad, m'kay?

C

Chris Angelico

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!

ChrisA
 
S

Steven D'Aprano

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*
 
C

Chris Angelico

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.

The XKCD does draw a distinction between assuming and asserting. And I
do say "for arg checking", which is the most common *abuse* of assert.
But mainly, I just like to share laughs :)

ChrisA
 
I

Irmen de Jong

Nice article.

BTW, what about:

if value >= 3:
raise AssertionError('value must be >= 3')

?

I don't think this qualifies as an assertion. Also, because AssertionError is documented
as "Raised when an assert statement fails", I would never use it myself explicitly like
this.

You should use ValueError instead (or a more precise exception such as IndexError, if
appropriate).

Irmen
 
S

Steven D'Aprano

Nice article.

BTW, what about:

if value >= 3:
raise AssertionError('value must be >= 3')

?

The error message is misleading. But you've probably noticed that by
now :)

What about it? Since it's missing any context, it could be a good use of
an exception or a terrible use. Where does value come from? Why is there
a restriction on the value?

As I see it, there are likely two reasons for writing such a test:

1) You're testing a value that comes from the user, or some
library you don't control; or

2) You're testing some internal invariant, a contract between
two parts of your own code, a piece of internal logic, etc.


In the first case, I don't think you should raise AssertionError. A
ValueError would be more appropriate.

In the second case, using an assert might be better, since that gives you
the opportunity to remove it at compile-time, if you choose.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top