logic behind the assert syntax?

Greg Landrum glandrum at my-deja.com
Mon Aug 28 18:11:37 EDT 2000


Today, whilst writing some testing code, I ran across the following
little peculiarity with assert:
 >>> assert 1==0,'foo'
 Traceback (innermost last):
   File "<stdin>", line 1, in ?
 AssertionError: foo
That's what I expect, but this::
 >>> assert (1==0,'foo')
 >>>
strikes me as a bit odd.

Based upon an analogy to the print statement, I understand what is
happening here:  assert sees a tuple when you put parens around it
and decides everything is hunky dory.  I don't like it, but I think
that I get it.

Of course, I get completely confused again when I try and apply my
newfound "understanding" to other statements.

so
assert 1,'foo'
and
assert (1,'foo')
do different things.

print 1,'foo'
and
print (1,'foo')
do different things.

but
return 1,'foo'
and
return (1,'foo')
do the same thing.

also
a = 1,'foo'
and
a = (1,'foo')
are equivalent.

weird.

continuing to screw around, I tried:
 >>> (1,'foo') == 1,'foo'
 (0, 'foo')
which seemed odd... so I tried:
 >>> (1,'foo') == (1,'foo')
 1
this made sense.  But then:
 >>> 1,'foo' == 1,'foo'
 (1, 0, 'foo')
confused me again until I realized that this was:
 1,('foo==1),'foo

Now my head is in a bit of a whirl...  Python normally strikes me as
so logical and reasonable, but this has thrown me for a loop.

Can someone clear this up a bit?

thanks!
-greg





Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list