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

Weird expression result

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 ?

George
Aug 18 '08 #1
7 1356
George Sakkis wrote:
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 ?

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

Peter
Aug 18 '08 #2
On Mon, 18 Aug 2008 18:04:32 +0200, Peter Otten wrote:
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
Aug 18 '08 #3
On Aug 18, 5:57 pm, George Sakkis <george.sak...@gmail.comwrote:
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 ?

George
>(3 in [3]) == True
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...
>3 in ([3] == 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.
>>a = list()
a.append(3)
3 in a
True
>>if a:
.... print "yay"
....
yay
>>if a == True:
.... print "yay"
.... else:
.... print "nay"
....
nay
>3 in [3] == True
http://docs.python.org/ref[3/summary.html
>>3 in [3] == True
False
>>3 in [3] == False
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...
Aug 18 '08 #4
Dan Lenski wrote:
On Mon, 18 Aug 2008 18:04:32 +0200, Peter Otten wrote:
>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
Aug 18 '08 #5
On Aug 18, 12:04 pm, Peter Otten <__pete...@web.dewrote:
George Sakkis wrote:
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 ?
George

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
Aug 18 '08 #6
co*********@gmail.com wrote:
>>>>3 in [3] == True

http://docs.python.org/ref[3/summary.html
that page is broken, as recently mentioned; "in", "not in", "is", and
"is not" are comparison operators too, chains in the same way as the
others. for details, see:

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

</F>

Aug 18 '08 #7
On Mon, 18 Aug 2008 18:27:53 +0200, Peter Otten wrote:
Dan Lenski wrote:
>How does this play with standard precedence rules?

Simple, all comparisons have the same priority:

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

Peter
I see. So, since the comparison operators have lower precedence than
everything else... it won't affect any terms involving non-comparison
operators.

Dan
Aug 18 '08 #8

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

Similar topics

3
by: redneck_kiwi | last post by:
Hi all: I have a really weird problem. I am developing a customer catalog system for my company and as such have delved into sessions for authentication and access levels. So far, I have managed...
8
by: Ganesan Rajaraman | last post by:
In the following examples, why the first case statement works and why the second doesn't, I wonder. Could anyone shed some light on this? Thanks, Ganesh -- this is an attempt to: -- ...
5
by: Gas | last post by:
I am new to C#, I want to perfoem a calculation and I have a weird out come from the app. Here is the code double percentage = 0; percentage = ctrl1.Height / (ctrl1.Height+ ctrl2.Height) ...
6
by: Chris Dunaway | last post by:
Consider this code (.Net 2.0) which uses a nullable type: private void button1_Click(object sender, System.EventArgs e) { DateTime? nullableDate; nullableDate = (condition) ? null :...
10
by: Bonj | last post by:
Hello. I hope somebody can help me on this, because I'm running out of options to turn to. I have almost solved my regular expression function. Basically it works OK if unicode is defined. It...
29
by: Ancient_Hacker | last post by:
A while back I had to recompile some old code, originally written by a really good programmer, but one prone to use every trick in the book, and then some. There was a statement, something like...
22
by: Daniel Rucareanu | last post by:
I have the following script: function Test(){} Test.F = function(){} Test.F.FF = function(){} Test.F.FF.FFF = function(){} Test.F.FF.FFF.FFFF = function(){} //var alias = function(){}; var...
2
by: vlsidesign | last post by:
Here is my a portion of my program: #include <stdio.h> main() { int fahr, celsius; int lower, upper, step; int fc_conv(int fahr); ... snip ... }
2
by: Chris | last post by:
Hi there, I have been reading in this group for a while really enjoy this pool of infinite c wisdom. :) Anyway, I have question now. I have this little piece of code that is giving me a...
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: 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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.