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


Groups > comp.lang.python > #59727

Re: When to use assert

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: When to use assert
Date 2013-11-17 08:33 -0500
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-8BDD06.08334817112013@news.panix.com> (permalink)
References <528871d5$0$29975$c3e8da3$5496439d@news.astraweb.com>

Show all headers | View raw


In article <528871d5$0$29975$c3e8da3$5496439d@news.astraweb.com>,
 Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:

> * Don't use assert for any error which you expect to recover from.
>   In other words, you've got no reason to catch an AssertionError
>   exception in production code.

Which leads to another reason for using asserts...

Every once in a while, I'll get into a situation where something is 
happening that I just can't understand.  If a given pice of code is 
being called, there's NO WAY the program should be exhibiting the 
behavior it's exhibiting.  But, there's also NO WAY that piece of code 
can't be getting called.

So, I stick "assert 0" in the code an re-run the program to see if I get 
an AssertionError.  If I do, then I know the code is being run.  If I 
don't then I know it's not.  Either way, I know more about what's going 
on than I did before.  Once I know what's going on, I remove the assert.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

When to use assert Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-17 07:35 +0000
  Re: When to use assert Tim Chase <python.list@tim.thechases.com> - 2013-11-17 06:50 -0600
    Re: When to use assert Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-18 00:52 +0000
  Re: When to use assert Roy Smith <roy@panix.com> - 2013-11-17 08:33 -0500
    Re: When to use assert Robert Day <robertkday@gmail.com> - 2013-11-18 08:20 +0000
    Re: When to use assert Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-11-18 09:50 +0000
    Re: When to use assert Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-18 10:02 +0000
    Re: When to use assert Tim Chase <python.list@tim.thechases.com> - 2013-11-18 05:44 -0600

csiph-web