Assertions in python

The syntax is:

assert <boolean>, ["error message"]

Error message is optional. Here’s an example:

a = 1
assert a == 1, "Nope, a is not 1" 
assert a == 2, "Nope, a is not 2" 

The result is:

Traceback (most recent call last):
File "assert.py", line 3, in <module>
assert a == 2, "no, a is not 2"
AssertionError: no, a is not 2

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s