I love assert

Anton anschatten at gmail.com
Wed Nov 12 23:00:24 CET 2014


On Wednesday, November 12, 2014 1:41:20 PM UTC-8, Marko Rauhamaa wrote:
> Asserts help the reader of the code understand why some possibilities
> are not considered by the code. They are not considered because the
> writer of the code asserts they are not really possible.
I can see how assert statement can point out what should not happen, can you give an example. 

> I use asserts sparingly, just like comments (which asserts are). I use
> them to communicate nonobvious truths to the future maintainer of the
> code.
> 
> Or I might indicate the exhaustion of possibilities:
> 
>      if status == OK:
>          ...
>      elif status == ERROR:
>          ...
>      else:
>          assert status == WARNING
>          ...
Could you elaborate your example here? Basically, this type of error checking does not look familiar to me in Python code. In C when one works with descriptors and has to check status codes after every call it looks natural. Maybe I just haven't used it this way, so I'd really appreciate a more specific use case.




More information about the Python-list mailing list