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

Discussion in 'Python' started by Thomas Guettler, Feb 23, 2005.

  1. Hi,

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

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


    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

    --
    Thomas Güttler, http://www.thomas-guettler.de/
    Thomas Guettler, Feb 23, 2005
    #1
    1. Advertising

  2. > "Thomas Guettler" <> wrote in message
    > news:p...
    > 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/
    Daniel Fackrell, Feb 23, 2005
    #2
    1. Advertising

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

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

    >
    >
    > 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
    Scott David Daniels, Feb 23, 2005
    #3
  4. Thomas Guettler

    Carl Banks Guest

    Thomas Guettler wrote:
    > 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.


    --
    CARL BANKS
    Carl Banks, Feb 23, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    374
    John Roth
    Jul 29, 2005
  2. Alex Vinokur

    assert(x) and '#define ASSERT(x) assert(x)'

    Alex Vinokur, Nov 25, 2004, in forum: C Programming
    Replies:
    5
    Views:
    893
    Keith Thompson
    Nov 25, 2004
  3. .rhavin grobert

    vector: Foo[5] == ((foo*)Foo) + 5 ?

    .rhavin grobert, Sep 23, 2008, in forum: C++
    Replies:
    4
    Views:
    381
    JaredGrubb
    Sep 24, 2008
  4. timr
    Replies:
    1
    Views:
    114
    Colin Bartlett
    Aug 23, 2010
  5. Replies:
    4
    Views:
    133
    Thomas 'PointedEars' Lahn
    Dec 23, 2007
Loading...

Share This Page