473,385 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
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/
Jul 18 '05 #1
3 3941
> "Thomas Guettler" <gu*****@thomas-guettler.de> wrote in message
news:pa****************************@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/


Jul 18 '05 #2
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
Sc***********@Acm.Org
Jul 18 '05 #3

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

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: David D. | last post by:
Does the file extension matter when including a JavaScript file in an HTML page? Normally, one would include a JavaScript file in an HTML page using <script src="foo.JS" type="text/javascript">...
0
by: Daniel | last post by:
how to make sure a xsl document has valid xsl syntax? i tried loading it into an xml document but that doesnt show syntax errors inside attributes such as "foo/bar" vs "bar\foo"
5
by: vilhelm.sjoberg | last post by:
Hello, I am a resonably confident C programmer, but not very sure about the dark corners of C++. Recently, I had G++ give me a strange error. The program in question is in essence: struct...
37
by: Rajesh | last post by:
I was informed tht $SUBJECT was asked in M$ interview. Real imp. is no. of ways to achive it. mine sol. was.... int main() { if(printf("foo")) { ; } else { ; }
8
by: Mark | last post by:
Could someone provide me with details or a link on how the line of code executes underneath the hood? Assume it's executed in an ASP.NET application. string blah =...
10
by: Paul Rubin | last post by:
So I have some assert statements in my code to verify the absence of some "impossible" conditions. They were useful in debugging and of course I left them in place for "real" runs of the program. ...
1
by: dhtmlkitchen | last post by:
Why does "foo" instanceof String return false? It just seems counterintuitive to me. I mean, sure, I can always check the constructor: var fooVar = "foo"; var isString =...
30
by: kj | last post by:
My book (Flanagan's JavaScript: The Definitive Guide, 5th ed.) implies on page 111 that the following two constructs are equivalent: ( x.constructor == Foo ) and ( x instanceof Foo ) The...
1
by: Sin Jeong-hun | last post by:
Hello. I used the same CookieContainer for several consecutive WebRequests, then I could see that it contains cookies for several domains. I examined the private m_domains and it contains a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.