Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #67980 > unrolled thread

Assertions are bad, m'kay?

Started byChris Angelico <rosuav@gmail.com>
First post2014-03-07 18:16 +1100
Last post2014-03-08 11:22 +1100
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Assertions are bad, m'kay? Chris Angelico <rosuav@gmail.com> - 2014-03-07 18:16 +1100
    Re: Assertions are bad, m'kay? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-07 11:11 +0000
      Re: Assertions are bad, m'kay? Chris Angelico <rosuav@gmail.com> - 2014-03-07 22:24 +1100
      Re: Assertions are bad, m'kay? Dan Stromberg <drsalists@gmail.com> - 2014-03-07 16:15 -0800
        Re: Assertions are bad, m'kay? Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2014-03-08 01:26 +0100
        Re: Assertions are bad, m'kay? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-08 14:44 +0000
      Re: Assertions are bad, m'kay? Ben Finney <ben+python@benfinney.id.au> - 2014-03-08 11:22 +1100

#67980 — Assertions are bad, m'kay?

FromChris Angelico <rosuav@gmail.com>
Date2014-03-07 18:16 +1100
SubjectAssertions are bad, m'kay?
Message-ID<mailman.7890.1394176617.18130.python-list@python.org>
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

[toc] | [next] | [standalone]


#67990

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-03-07 11:11 +0000
Message-ID<5319a975$0$29985$c3e8da3$5496439d@news.astraweb.com>
In reply to#67980
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/

[toc] | [prev] | [next] | [standalone]


#67991

FromChris Angelico <rosuav@gmail.com>
Date2014-03-07 22:24 +1100
Message-ID<mailman.7897.1394191472.18130.python-list@python.org>
In reply to#67990
On Fri, Mar 7, 2014 at 10:11 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
>> 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.

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

[toc] | [prev] | [next] | [standalone]


#68009

FromDan Stromberg <drsalists@gmail.com>
Date2014-03-07 16:15 -0800
Message-ID<mailman.7909.1394237738.18130.python-list@python.org>
In reply to#67990
On Fri, Mar 7, 2014 at 3:11 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:

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

> You should read this guy's blog post on when to use assert:
>
> http://import-that.dreamwidth.org/676.html

Nice article.

BTW, what about:

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

?

[toc] | [prev] | [next] | [standalone]


#68011

FromIrmen de Jong <irmen.NOSPAM@xs4all.nl>
Date2014-03-08 01:26 +0100
Message-ID<531a639b$0$2843$e4fe514c@news.xs4all.nl>
In reply to#68009
On 8-3-2014 1:15, Dan Stromberg wrote:
> On Fri, Mar 7, 2014 at 3:11 AM, Steven D'Aprano
> <steve+comp.lang.python@pearwood.info> wrote:
> 
>>
>> Assertions are not bad! They're just misunderstood and abused.
> 
>> You should read this guy's blog post on when to use assert:
>>
>> http://import-that.dreamwidth.org/676.html
> 
> 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



[toc] | [prev] | [next] | [standalone]


#68041

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-03-08 14:44 +0000
Message-ID<531b2cc7$0$29985$c3e8da3$5496439d@news.astraweb.com>
In reply to#68009
On Fri, 07 Mar 2014 16:15:36 -0800, Dan Stromberg wrote:

> On Fri, Mar 7, 2014 at 3:11 AM, Steven D'Aprano
> <steve+comp.lang.python@pearwood.info> wrote:
> 
> 
>> Assertions are not bad! They're just misunderstood and abused.
> 
>> You should read this guy's blog post on when to use assert:
>>
>> http://import-that.dreamwidth.org/676.html
> 
> 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.




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

[toc] | [prev] | [next] | [standalone]


#68010

FromBen Finney <ben+python@benfinney.id.au>
Date2014-03-08 11:22 +1100
Message-ID<mailman.7910.1394238163.18130.python-list@python.org>
In reply to#67990
Dan Stromberg <drsalists@gmail.com> writes:

> BTW, what about:
>
> if value >= 3:
>    raise AssertionError('value must be >= 3')

That would be very confusing, since it would only appear when the value
is >= 3. Were you making some other point?

-- 
 \        “If this is your first visit to the USSR, you are welcome to |
  `\                                          it.” —hotel room, Moscow |
_o__)                                                                  |
Ben Finney

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web