Here are four simple invocations of assert:
>>> assert 1==2
Traceback (most recent call last):
File \"\", line 1, in ?
AssertionError
&g
Following is cited from the python doc
Assert statements are a convenient way to insert debugging assertions into a program:
assert_stmt ::= "assert" expression ["," expression]
The simple form, assert expression, is equivalent to
if __debug__:
if not expression: raise AssertionError
The extended form, assert expression1, expression2, is equivalent to
if __debug__:
if not expression1: raise AssertionError(expression2)
So when you're using parenthesis here, you're using the simple form, and the expression is evaluated as a tuple, which is always True when being casted to bool
assert 1==2, "hi"
is parsed as assert 1==2, "hi"
with "hi" as the second parameter for the keyword. Hence why it properly gives an error.
assert(1==2)
is parsed as assert (1==2)
which is identical to assert 1==2
, because parens around a single item don't create a tuple unless there's a trailing comma e.g. (1==2,)
.
assert(1==2, "hi")
is parsed as assert (1==2, "hi")
, which doesn't give an error because a non-empty tuple (False, "hi")
isn't a false value, and there is no second parameter supplied to the keyword.
You shouldn't use parentheses because assert
is not a function in Python - it's a keyword.
The last assert
would have given you a warning (SyntaxWarning: assertion is always true, perhaps remove parentheses?
) if you ran it through a full interpreter, not through IDLE. Because assert
is a keyword and not a function, you are actually passing in a tuple as the first argument and leaving off the second argument.
Recall that non-empty tuples evaluate to True
, and since the assertion message is optional, you've essentially called assert True
when you wrote assert(1==2, "hi")
.
You can break assert statement without \
like this:
foo = 7
assert foo == 8, (
'derp should be 8, it is ' + str(foo))
Or if you have even longer message:
foo = 7
assert foo == 8, (
'Lorem Ipsum is simply dummy text of the printing and typesetting '
'industry. Lorem Ipsum has been the industry\'s standard dummy text '
'ever since the 1500s'
)
If you put the parenthesis in there because you wanted a multi-line assert, then an alternative is to put a backslash at the end of the line like this:
foo = 7
assert foo == 8, \
"derp should be 8, it is " + str(foo)
Prints:
AssertionError: "derp should be 8, it is 7
assert
have to be different from everything else:I think the pythonic ideology is that a program should self-correct without having to worry about the special flag to turn on asserts. The temptation to turn off asserts is too great, and thus it's being deprecated.
I share your annoyance that the python assert
has unique syntax relative to all other python programming constructs, and this syntax has yet again changed from python2 to python3 and again changed from python 3.4 to 3.6.
Making assert statements not backward compatible from any version to any other version.
It's a tap on the shoulder that assert
is a 3rd class citizen, it will be totally removed in python4, and certainly again in Python 8.1.