Created
February 15, 2016 18:31
-
-
Save orbeckst/9e46d0653bf20fa0172d to your computer and use it in GitHub Desktop.
demonstrate that `python -O` removes `assert`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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