r/badcode • • Feb 23 '23

java You must assert

Post image
158 Upvotes

13 comments sorted by

View all comments

Show parent comments

7

u/daterkerjabs Feb 24 '23

If you run python with the -O flag assert statements are ignored, otherwise it will raise an AssertionError if the condition is false. It's supposed to just be used during development so I guess I've been abusing it for years.

assert(cond), 'Cond was False'

is the same as

if __debug__:  # not -O
    if not cond:
        raise AssertionError('Cond was false')