Understanding someone code and met this:
There is a function:
def some_func():
print("Hello")
assert(len(y)==len(p))
print("First assert")
assert(p.all()<=0.99999)
print("Second assert")
return 1
Next, call assert_raises:
np.testing.assert_raises(AssertionError, some_func, np.asarray([1, 2, 3]), np.asarray([1, 2, 3, 4, 5]))
In the output, we simply get Hello without exception messages:
Hello
Next, call the function assert_array_less:
np.testing.assert_array_less(some_func(np.asarray([1, 2, 3]), np.asarray([1, 2, 3])), np.inf)
In the output, we get Hello First assert and then an error message and an AssertionError exception:
Hello
First assert
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-26-df1a32b4f5a0> in <module>()
9 np.testing.assert_raises(AssertionError, some_func, np.asarray([1, 2, 3]), np.asarray([1, 2, 3, 4, 5]))
10
---> 11 np.testing.assert_array_less(some_func(np.asarray([1, 2, 3]), np.asarray([1, 2, 3])), np.inf)
<ipython-input-26-df1a32b4f5a0> in some_func(a, b)
3 assert(len(a)==len(b))
4 print("First assert")
----> 5 assert(a.all()<=0.99999)
6 print("Second assert")
7 return 1
AssertionError:
Question:
Why in 1 case the code just stops and no exceptions are thrown, although it is called first assert in some_func ()?
And why in the second does not happen the same as in the first, and an exception is thrown?