473,386 Members | 1,766 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,386 software developers and data experts.

Am I stupid or is 'assert' broken in Python 2.5??

I've noticed something odd in Python 2.5, namely that the 2 argument
version of 'assert' is broken. Or at least it seems that way to me.

Run the following code in your Python interpreter:

myString = None

assert( myString, 'The string is either empty or set to the None type!'
)
assert( myString )

You'll notice that the first assert doesn't do anything, whereas the
second assert correctly recognizes that myString does not evaluate to
true. That doesn't seem right. Surely Python should have raised an
assertion error on the first assert statement, right??

Dec 6 '06 #1
11 2082
Never mind, I'm a schmuck!! =0

It should have been
assert myString, 'String empty or None!'
Sorry, ignore me. =\

Dec 6 '06 #2
antred a écrit :
I've noticed something odd in Python 2.5, namely that the 2 argument
version of 'assert' is broken. Or at least it seems that way to me.

Run the following code in your Python interpreter:

myString = None

assert( myString, 'The string is either empty or set to the None
type!' ) assert( myString )

You'll notice that the first assert doesn't do anything, whereas the
second assert correctly recognizes that myString does not evaluate to
true. That doesn't seem right. Surely Python should have raised an
assertion error on the first assert statement, right??
That behaviour has been present in Python for a long time. Just know
that assert is NOT a function, and thus it doesn't require parenthesis (
just the same as print doesn't require them ) Try without them and it'll
work.
Dec 6 '06 #3
In <11**********************@73g2000cwn.googlegroups. com>, antred wrote:
Run the following code in your Python interpreter:

myString = None

assert( myString, 'The string is either empty or set to the None type!'
)
assert( myString )

You'll notice that the first assert doesn't do anything, whereas the
second assert correctly recognizes that myString does not evaluate to
true. That doesn't seem right. Surely Python should have raised an
assertion error on the first assert statement, right??
``assert`` is a statement, not a function. And non-empty tuples are "true":

assert (False, 'boink')

This is equivalent to ``assert True``.

Ciao,
Marc 'BlackJack' Rintsch
Dec 6 '06 #4
antred wrote:
I've noticed something odd in Python 2.5, namely that the 2 argument
version of 'assert' is broken. Or at least it seems that way to me.

Run the following code in your Python interpreter:

myString = None

assert( myString, 'The string is either empty or set to the None type!'
)
assert( myString )

You'll notice that the first assert doesn't do anything, whereas the
second assert correctly recognizes that myString does not evaluate to
true. That doesn't seem right. Surely Python should have raised an
assertion error on the first assert statement, right??
No, the parens turn (myString, '...') into a single (non-False) tuple
argument to the assert *statement*.
>>assert (None, None)
assert None, None
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AssertionError

Peter
Dec 6 '06 #5
antred a écrit :
I've noticed something odd in Python 2.5, namely that the 2 argument
version of 'assert' is broken. Or at least it seems that way to me.

Run the following code in your Python interpreter:

myString = None

assert( myString, 'The string is either empty or set to the None type!'
)
assert( myString )

You'll notice that the first assert doesn't do anything, whereas the
second assert correctly recognizes that myString does not evaluate to
true. That doesn't seem right. Surely Python should have raised an
assertion error on the first assert statement, right??
What you see is correct. Here is a test under Python 2.4:
>>myString = None
assert myString, "It is empty"
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AssertionError: It is empty
>>assert (myString, "It is empty")
If you use parenthesis to group the condition to test and the error
message, then the whole created tuple is used as a condition to test...
and this condition is true so assert dont raise any exception.

So, just remove your parenthesis and all will go the expected way.

A+

Laurent.
Dec 6 '06 #6
Yeah, it hit me seconds after I had posted my message. =0 Why didn't I
think of it during the 30 minutes I spent banging my head against the
keyboard going nuts over this 'bug' ...

Dec 6 '06 #7
The reason why it won't raise the AssertionError is because the
condition in the assert statement is a non-empty tuple, and its boolean
value would be True, not False, which is required to raise an assertion
error.

antred wrote:
Yeah, it hit me seconds after I had posted my message. =0 Why didn't I
think of it during the 30 minutes I spent banging my head against the
keyboard going nuts over this 'bug' ...
Dec 6 '06 #8

"antred" <Nu****@gmx.netwrote in message
news:11*********************@16g2000cwy.googlegrou ps.com...
Yeah, it hit me seconds after I had posted my message. =0 Why didn't I
think of it during the 30 minutes I spent banging my head against the
keyboard going nuts over this 'bug' ...
Because communication brings consciousness ;-)

Dec 6 '06 #9
At Wednesday 6/12/2006 11:46, antred wrote:
>Yeah, it hit me seconds after I had posted my message. =0 Why didn't I
think of it during the 30 minutes I spent banging my head against the
keyboard going nuts over this 'bug' ...
The same reason you can sometimes find what's wrong just by
explaining the symptoms to another guy... Having to put things sorted
and simple to understand by another, just makes you think clearly on
the problem...
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
Dec 7 '06 #10
On 2006-12-07, Gabriel Genellina <ga******@yahoo.com.arwrote:
>>Yeah, it hit me seconds after I had posted my message. =0 Why
didn't I think of it during the 30 minutes I spent banging my
head against the keyboard going nuts over this 'bug' ...

The same reason you can sometimes find what's wrong just by
explaining the symptoms to another guy... Having to put things
sorted and simple to understand by another, just makes you
think clearly on the problem...
I read a story (was it by Brian Kernighan?) the they required
programmers to tell their problem to a stuffed animal first
before bothering another programmer who might be in the middle of
something. The stuffed animal often provided all the assistance
that was needed.

--
Neil Cerutti
Dec 7 '06 #11
Neil Cerutti <ho*****@yahoo.comwrites:
On 2006-12-07, Gabriel Genellina <ga******@yahoo.com.arwrote:
The same reason you can sometimes find what's wrong just by
explaining the symptoms to another guy... Having to put things
sorted and simple to understand by another, just makes you think
clearly on the problem...

I read a story (was it by Brian Kernighan?) the they required
programmers to tell their problem to a stuffed animal first before
bothering another programmer who might be in the middle of
something. The stuffed animal often provided all the assistance that
was needed.
This is now known as "rubber ducking", and it was indeed documented in
"The Pragmatic Programmer", a highly recommended tome.

<URL:http://c2.com/cgi/wiki?RubberDucking>

--
\ "Even if the voices in my head are not real, they have pretty |
`\ good ideas." -- Anonymous |
_o__) |
Ben Finney

Dec 7 '06 #12

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

Similar topics

28
by: Fábio Mendes | last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with things or the python-list sent my msg to /dev/null. I couldn't find anything related in previous PEP's, so here it goes a...
3
by: Thomas Guettler | last post by:
Hi, Python 2.3.3 (#1, Feb 5 2005, 16:22:10) on linux2 >>> assert 0, "foo" Traceback (most recent call last): File "<stdin>", line 1, in ? AssertionError: foo >>> assert(0, "foo") >>>
8
by: Jan Danielsson | last post by:
Hello all, How do I make a python script actually a _python_ in unix:ish environments? I know about adding: #!/bin/sh ..as the first row in a shell script, but when I installed python on...
13
by: Matthias Kaeppler | last post by:
Hi, why can I use assert from <cassert> without resolving the std:: namespace? E.g.: #include <cassert> int main() { assert(true); // shouldn't this be: std::assert(true) ?
47
by: Rob Thorpe | last post by:
In general, is it considered bad practice to use asserts in production code? What about writing a macro that does the same as assert but continues to work regardless of the state of NDEBUG? I...
28
by: lovecreatesbeauty | last post by:
Besides printing out for example " a.out: p113.c:8: main: Assertion `0' failed. Aborted " and a switch option NDEBUG, what other benefits does assert() provide in any scope of designing,...
15
by: Jim B. Wilson | last post by:
Am I nuts? Or only profoundly confused? I expected the this little script to print "0": class foo(int): def __init__(self, value): self = value & 0xF print foo(0x10) Instead, it prints...
11
by: Francois Grieu | last post by:
Hi, I'm using an assert()-like macro to test a constant expression at compile time #define ASSERT(condition) struct{char assert_failure;} The idea is that this macro cause a compilation...
9
by: pereges | last post by:
Ok, so once I'm done debugging my code(split across multiple modules) using the assert macro, I would want to switch off all the assert macros ued in the program. Does this mean I have to include:...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.