assert 0, "foo" vs. assert(0, "foo")

Daniel Fackrell unlearned at gmail.com
Wed Feb 23 05:25:50 EST 2005


> "Thomas Guettler" <guettli at thomas-guettler.de> wrote in message
> news:pan.2005.02.23.09.57.43.578480 at thomas-guettler.de...
> Hi,

> Python 2.3.3 (#1, Feb  5 2005, 16:22:10) [GCC 3.3.3 (SuSE Linux)] on
linux2
> >>> assert 0, "foo"

Assert that 0 is true.  If that fails, raise AssertionError("foo").

> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> AssertionError: foo
> >>> assert(0, "foo")

Assert that the tuple (0, "foo") is true.  Non-empty tuples are always true.

> >>>
>
> If you use parenthesis for the assert statement, it never
> raises an exception.

> Up to now I raised strings, but since this is deprecated,
> I switched to use the second argument for the assert
> statement.

> Is it possible to change future python versions, that
> assert accept parenthesis?

As shown above, it does, but it doesn't do quite what you expected.  For
further enlightenment, try the following and think through why each one
gives the results it does:

assert (), 'spam'
assert [], 'eggs'
assert {}, 'spam and eggs'
assert (0,), 'spam, spam, and eggs'
assert (0, "foo"), 'spam, spam, eggs, and spam'
assert 0, "foo", 'shrubbery'

The last will give a syntax error.  Can you spot why?

>  Thomas

> -- 
> Thomas Güttler, http://www.thomas-guettler.de/






More information about the Python-list mailing list