Am I stupid or is 'assert' broken in Python 2.5??
Laurent Pointal
laurent.pointal at limsi.fr
Wed Dec 6 09:44:58 EST 2006
antred a écrit :
> I've noticed something odd in Python 2.5, namely that the 2 argument
> version of 'assert' is broken. Or at least it seems that way to me.
>
> Run the following code in your Python interpreter:
>
> myString = None
>
> assert( myString, 'The string is either empty or set to the None type!'
> )
> assert( myString )
>
> You'll notice that the first assert doesn't do anything, whereas the
> second assert correctly recognizes that myString does not evaluate to
> true. That doesn't seem right. Surely Python should have raised an
> assertion error on the first assert statement, right??
What you see is correct. Here is a test under Python 2.4:
>>> myString = None
>>> assert myString, "It is empty"
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AssertionError: It is empty
>>> assert (myString, "It is empty")
If you use parenthesis to group the condition to test and the error
message, then the whole created tuple is used as a condition to test...
and this condition is true so assert dont raise any exception.
So, just remove your parenthesis and all will go the expected way.
A+
Laurent.
More information about the Python-list
mailing list