Skip to content

Instantly share code, notes, and snippets.

@orbeckst
Created February 15, 2016 18:31
Show Gist options
  • Save orbeckst/9e46d0653bf20fa0172d to your computer and use it in GitHub Desktop.
Save orbeckst/9e46d0653bf20fa0172d to your computer and use it in GitHub Desktop.
demonstrate that `python -O` removes `assert`
# test assert vs assert_
# run with
# python assert_test.py
# and
# python -O assert_test.py
#
# to see that assert is optimized away by -O
from numpy.testing import assert_
def test_py_assert():
assert 1 == 2, "assert found an error"
def test_np_assert():
assert_(1 == 2, "assert_ found an error")
if __name__ == "__main__":
try:
test_py_assert()
except AssertionError as err:
print(err)
else:
print("py_assert did not raise exception but it should have!")
try:
test_np_assert()
except AssertionError as err:
print(err)
else:
print("np_assert did not raise exception but it should have!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment