Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 18th, 2005, 10:56 PM
Thomas Guettler
Guest
 
Posts: n/a
Default assert 0, "foo" vs. assert(0, "foo")

Hi,

Python 2.3.3 (#1, Feb 5 2005, 16:22:10) [GCC 3.3.3 (SuSE Linux)] on linux2[color=blue][color=green][color=darkred]
>>> assert 0, "foo"[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AssertionError: foo[color=blue][color=green][color=darkred]
>>> assert(0, "foo")
>>>[/color][/color][/color]

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/


  #2  
Old July 18th, 2005, 10:56 PM
Daniel Fackrell
Guest
 
Posts: n/a
Default Re: assert 0, "foo" vs. assert(0, "foo")

> "Thomas Guettler" <guettli@thomas-guettler.de> wrote in message[color=blue]
> news:pan.2005.02.23.09.57.43.578480@thomas-guettler.de...
> Hi,[/color]
[color=blue]
> Python 2.3.3 (#1, Feb 5 2005, 16:22:10) [GCC 3.3.3 (SuSE Linux)] on[/color]
linux2[color=blue][color=green][color=darkred]
> >>> assert 0, "foo"[/color][/color][/color]

Assert that 0 is true. If that fails, raise AssertionError("foo").
[color=blue]
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> AssertionError: foo[color=green][color=darkred]
> >>> assert(0, "foo")[/color][/color][/color]

Assert that the tuple (0, "foo") is true. Non-empty tuples are always true.
[color=blue][color=green][color=darkred]
> >>>[/color][/color]
>
> If you use parenthesis for the assert statement, it never
> raises an exception.[/color]
[color=blue]
> Up to now I raised strings, but since this is deprecated,
> I switched to use the second argument for the assert
> statement.[/color]
[color=blue]
> Is it possible to change future python versions, that
> assert accept parenthesis?[/color]

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?
[color=blue]
> Thomas[/color]
[color=blue]
> --
> Thomas Güttler, http://www.thomas-guettler.de/[/color]



  #3  
Old July 18th, 2005, 10:57 PM
Scott David Daniels
Guest
 
Posts: n/a
Default Re: assert 0, "foo" vs. assert(0, "foo")

Thomas Guettler wrote:[color=blue]
> Hi,
>
> Python 2.3.3 (#1, Feb 5 2005, 16:22:10) [GCC 3.3.3 (SuSE Linux)] on linux2
>[color=green][color=darkred]
>>>>assert 0, "foo"[/color][/color]
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> AssertionError: foo
>[color=green][color=darkred]
>>>>assert(0, "foo")
>>>>[/color][/color]
>
>
> 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?[/color]
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.Daniels@Acm.Org
  #4  
Old July 18th, 2005, 10:57 PM
Carl Banks
Guest
 
Posts: n/a
Default Re: assert 0, "foo" vs. assert(0, "foo")


Thomas Guettler wrote:[color=blue]
> Is it possible to change future python versions, that
> assert accept parenthesis?[/color]


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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles