Assertions

jladasky at itu.edu jladasky at itu.edu
Fri Sep 22 17:16:26 EDT 2017


On Thursday, September 21, 2017 at 9:29:19 AM UTC-7, Tobiah wrote:
> Are these completely equivalent?
> 
> def foo(thing):
> 
>         assert(thing > 0), "Thing must be greater than zero"
> 
> 
> def foo(thing):
> 
>         if not (thing > 0): raise AssertionError("Thing must be greater than zero")
> 
> 
> Other than the fact that the assertion can be turned off
> with -O?

For that reason, I would prefer to raise ValueError instead of AssertionError.  The -O flag is nice for turning off test code.  If the code is essential to the functionality of the program, you probably don't want to misidentify it as test code.



More information about the Python-list mailing list