Am I stupid or is 'assert' broken in Python 2.5??

Jean-Paul Calderone exarkun at divmod.com
Wed Dec 6 09:41:43 EST 2006


On 6 Dec 2006 06:34:49 -0800, antred <nutjob at gmx.net> wrote:
>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??

assert is not a function, it's a keyword.  "assert x" raises an
AssertionError if "x" evaluates to false.  "(myString, "foo")"
is a two-tuple.  two-tuples are never false.

"(myString)" is the same as "myString".




More information about the Python-list mailing list