Best practice for Python Assert

Posted by meade on Stack Overflow See other posts from Stack Overflow or by meade
Published on 2009-06-03T12:57:16Z Indexed on 2010/04/09 13:53 UTC
Read the original article Hit count: 289

Filed under:
|
|
|

Is there a performance or code maintenance issue with using assert as part of the standard code instead of using it just for debugging purposes?

Is

assert x >= 0, 'x is less then zero'

and better or worse then

if x < 0:
    raise Exception, 'x is less then zero'

Also, is there anyway to set a business rule like if x < 0 raise error that is always checked with out the try, except, finally so, if at anytime throughout the code x is < 0 an error is raised, like if you set assert x < 0 at the start of a function, anywhere within the function where x becomes less then 0 an exception is raised?

© Stack Overflow or respective owner

Related posts about python

Related posts about assert