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

A

antred

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

antred

Never mind, I'm a schmuck!! =0

It should have been


assert myString, 'String empty or None!'


Sorry, ignore me. =\
 
C

Christophe

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.
 
M

Marc 'BlackJack' Rintsch

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
 
P

Peter Otten

antred said:
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*.
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AssertionError

Peter
 
L

Laurent Pointal

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:
Traceback (most recent call last):

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.
 
A

antred

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' ...
 
D

dakman

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.
 
T

Terry Reedy

antred said:
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 ;-)
 
G

Gabriel Genellina

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
 
N

Neil Cerutti

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.
 
B

Ben Finney

Neil Cerutti said:
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>
 

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,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top