Weird expression result

P

Peter Otten

D

Dan Lenski

This works just like a < b < c:
3 in [3] and [3] == True
False

Peter

Interesting. I agree with the OP that it is confusing!

Does this expansion really get invoked every time there is an expression
of the form??

expr1 binary_op1 expr2 binary_op2 expr3 ...

Seemingly, the answer is yes:
>>> 3 in [3] in [[3],[4]] in [[[3],[4]],5] == [[[3],[2+2]],5]
True

How does this play with standard precedence rules?

Dan
 
C

cokofreedom

I'm probably missing something obvious but I can't put my finger on
it:
(3 in [3]) == True
True
3 in ([3] == True)

Traceback (most recent call last):

False

How/why does the last one evaluate to False ?

George

The list holds an integer value of 3, you check if 3 is in that list,
that is true, you then check it your answer is true...

True and False are keywords now and do not correspond the way you
think. [3] == True or [3] == False will both answer False.

Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information..... print "yay"
....
yay.... print "yay"
.... else:
.... print "nay"
....
nay
False

However I come a bit stuck myself, since this should just tell you
that you cannot do a membership test on 3 being in a bool...but it
doesn't...It should be doing the [3] == True, returning false then
trying to see if 3 is in false...
 
P

Peter Otten

Dan said:
This works just like a < b < c:
3 in [3] and [3] == True False

Interesting. I agree with the OP that it is confusing!

Does this expansion really get invoked every time there is an expression
of the form??

expr1 binary_op1 expr2 binary_op2 expr3 ...

Seemingly, the answer is yes:
3 in [3] in [[3],[4]] in [[[3],[4]],5] == [[[3],[2+2]],5]
True

How does this play with standard precedence rules?

Simple, all comparisons have the same priority:

http://docs.python.org/ref/comparisons.html

Peter
 
G

George Sakkis

George said:
I'm probably missing something obvious but I can't put my finger on
it:
(3 in [3]) == True True
3 in ([3] == True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument of type 'bool' is not iterable
3 in [3] == True False

How/why does the last one evaluate to False ?

This works just like a < b < c:
3 in [3] and [3] == True

False

Peter


Argh, you're right! I think chaining makes sense only for comparison
operators, for anything else it's not obvious. That came up from a
real bug by the way, it's not made up.

George
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top