assert 는 assert False 일때 에러를 출력하는 function이다.
Try it in the Python shell:
>>> assert True
>>> assert False
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
Example )
assert(2 + 2 == 5, "Houston we've got a problem")
won't work, unlike
assert 2 + 2 == 5, "Houston we've got a problem"
The reason the first one doesn't work is that bool( (False, "Houston we've got a problem") )
evaluates to True
.
In the statement assert(False)
, these are just redundant parentheses around False
, which evaluate to their contents. But with assert(False,)
the parentheses are now a tuple, and a non-empty tuple evaluates to True in a boolean context.
==============================================================================================================
Reference
http://stackoverflow.com/questions/5142418/what-is-the-use-of-assert-in-python
'언어 > ㄴPython' 카테고리의 다른 글
[pyton] numpy.cmd (0) | 2016.01.21 |
---|---|
[python] generating random array (0) | 2016.01.21 |
[python] what is difference between numpy's 'array' and 'asarray' (0) | 2016.01.19 |
[python] basic python (0) | 2016.01.19 |
[python] 'from X import name' vs 'import X.name' (0) | 2016.01.17 |