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

T

Thomas Guettler

Hi,

Python 2.3.3 (#1, Feb 5 2005, 16:22:10) [GCC 3.3.3 (SuSE Linux)] on linux2Traceback (most recent call last):

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?

Thomas
 
D

Daniel Fackrell

Thomas Guettler said:
Python 2.3.3 (#1, Feb 5 2005, 16:22:10) [GCC 3.3.3 (SuSE Linux)] on linux2

Assert that 0 is true. If that fails, raise AssertionError("foo").
Traceback (most recent call last):

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?
 
S

Scott David Daniels

Thomas said:
Hi,

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

Traceback (most recent call last):


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?
You are confusing assert with raise.

assert test, text

behaves like:

if __debug__ and test:
raise AssertionError, text

As far as raise goes, where you have been writing:

raise "some complaint"

you could simply use:

raise ValueError, "some complaint"

or:

raise ValueError("some complaint")

--Scott David Daniels
(e-mail address removed)
 
C

Carl Banks

Thomas said:
Is it possible to change future python versions, that
assert accept parenthesis?


It's possible, but extremely unlikely that it will ever happen. assert
is not a function, but a statement (like print). Statements don't use
parentheses; when you use parentheses, it considers that a tuple.

For example, if you try this with print:

print ("hello","world")

you see that in prints out a tuple value, rather than treating "hello"
and "world" as arguments. Same thing with assert.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top