When to use assert

Robert Day robertkday at gmail.com
Mon Nov 18 09:20:33 CET 2013

On 17/11/13 13:33, Roy Smith wrote:
> 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. 

Are assertions the right thing in that case? I'm becoming more and more 
inclined to the idea that if you want to know whether code is getting 
run, you should put a debug log in, and leave it in. That way it's 
easier to track down the next bug (I don't know about others, but when I 
write buggy code I go all-out and put lots of bugs in rather than just one).


More information about the Python-list mailing list