assert

Date: January 3rd 2016
Last updated: January 3rd 2016

What is an assert statement? assert is used to test if a logical condition is true, and trigger an error if the condition is false.

From the py2 docs: "assert statements are a convenient way to insert debugging assertions into a program". That is, check to see if a particular statement evaluates to true.

# Input
assert 3 < 2, 'your equation is troubled'  

# Output (redacted)
# AssertionError: your equation is troubled

The assert function is similar to raise AssertionError.

# Input
if not 2 > 3:
    raise AssertionError('troubled waters')

# Output (redacted)
# AssertionError: troubled waters

It seems I have stumbled onto the next keyword I have been missing: raise. I obviously haven't been paying close enough attention to debugging etiquette. I will come back to this topic later.

Useful resources:

results matching ""

    No results matching ""