Can anyone please explain why these two give different results in Python
2.3? 'a' in 'abc' == 1
False ('a' in 'abc') == 1
True
I know it's not a good idea to compare boolean with Integer but that's
not the answer to my question.
--
Frantisek Fuka
(yes, that IS my real name)
(and it's pronounced "Fran-tjee-shek Foo-kah")
----------------------------------------------------
My E-mail: fu**@fuxoft.cz
My Homepage: http://www.fuxoft.cz
My ICQ: 2745855 7 2388
Frantisek Fuka wrote: Can anyone please explain why these two give different results in Python 2.3?
>>> 'a' in 'abc' == 1 False >>> ('a' in 'abc') == 1 True
I know it's not a good idea to compare boolean with Integer but that's not the answer to my question.
Hmm.... dis.dis(lambda : 'a' in 'abc' == 1)
1 0 LOAD_CONST 1 ('a')
3 LOAD_CONST 2 ('abc')
6 DUP_TOP
7 ROT_THREE
8 COMPARE_OP 6 (in)
11 JUMP_IF_FALSE 10 (to 24)
14 POP_TOP
15 LOAD_CONST 3 (1)
18 COMPARE_OP 2 (==)
21 JUMP_FORWARD 2 (to 26) 24 ROT_TWO
25 POP_TOP 26 RETURN_VALUE dis.dis(lambda : ('a' in 'abc') == 1)
1 0 LOAD_CONST 1 ('a')
3 LOAD_CONST 2 ('abc')
6 COMPARE_OP 6 (in)
9 LOAD_CONST 3 (1)
12 COMPARE_OP 2 (==)
15 RETURN_VALUE
Judging by the "odd" (unexpected) code in the first example,
"A in B == C" is actually doing operator chaining, in a manner
similar to "A < B < C". Since 'a' is in 'abc' but 'abc' is
not equal to 1, the chained test fails.
I can't comment further on the validity of such a thing, but
there you have it.
-Peter
"Frantisek Fuka" <fu**@fuxoft.cz> wrote in message
news:bn**********@ns.felk.cvut.cz... Can anyone please explain why these two give different results in Python 2.3?
>>> 'a' in 'abc' == 1 False >>> ('a' in 'abc') == 1 True
I know it's not a good idea to compare boolean with Integer but that's not the answer to my question.
According to the operator precedence table given in the
Python Reference Manual, section 5.14 (2.2.3 version)
the "==" operator has higher precidence (that is, it will
be evaluated first) than the "in" operator. The table is,
unfortunately, upside down from my perspective, but
a close examination of the explanation shows what is
happening.
In other words, your first expression is equivalent
to:
"a" in ("abc" == 1)
John Roth -- Frantisek Fuka (yes, that IS my real name) (and it's pronounced "Fran-tjee-shek Foo-kah") ---------------------------------------------------- My E-mail: fu**@fuxoft.cz My Homepage: http://www.fuxoft.cz My ICQ: 2745855
John Roth wrote:
... >>> 'a' in 'abc' == 1 False
... In other words, your first expression is equivalent to:
"a" in ("abc" == 1)
Nope: that would raise an exception rather than returning
False (try it!).
What's happening is *chaining* of relationals, just like
when you write e.g. a < b <= c. In such cases the effect
is line (a < b) and (b <= c) except that b is only evaluated
once. Similarly, the first expression above is equivalent
to
('a' in 'abc') and ('abc' == 1)
Alex
John Roth wrote: "Frantisek Fuka" <fu**@fuxoft.cz> wrote in message news:bn**********@ns.felk.cvut.cz...
Can anyone please explain why these two give different results in Python 2.3?
>>> 'a' in 'abc' == 1 False >>> ('a' in 'abc') == 1 True
I know it's not a good idea to compare boolean with Integer but that's not the answer to my question.
According to the operator precedence table given in the Python Reference Manual, section 5.14 (2.2.3 version) the "==" operator has higher precidence (that is, it will be evaluated first) than the "in" operator. The table is, unfortunately, upside down from my perspective, but a close examination of the explanation shows what is happening.
In other words, your first expression is equivalent to:
"a" in ("abc" == 1)
No, it is not, because the original expression produces "False" while
your expression produces "TypeError: iterable argument required".
--
Frantisek Fuka
(yes, that IS my real name)
(and it's pronounced "Fran-tjee-shek Foo-kah")
----------------------------------------------------
My E-mail: fu**@fuxoft.cz
My Homepage: http://www.fuxoft.cz
My ICQ: 2745855
"Alex Martelli" <al***@aleax.it> wrote in message
news:p%***********************@news2.tin.it... John Roth wrote: ... >>> 'a' in 'abc' == 1 False ... In other words, your first expression is equivalent to:
"a" in ("abc" == 1)
Nope: that would raise an exception rather than returning False (try it!).
What's happening is *chaining* of relationals, just like when you write e.g. a < b <= c. In such cases the effect is line (a < b) and (b <= c) except that b is only evaluated once. Similarly, the first expression above is equivalent to ('a' in 'abc') and ('abc' == 1)
You're right. Section 5.9 says this, and directly contradicts
section 5.14 as well. 5.14 shows "in" as having a lower
priority than "==", while the verbiage n 5.9 says they
have the same priority.
One or the other should be corrected.
John Roth
Alex
John Roth wrote:
... You're right. Section 5.9 says this, and directly contradicts section 5.14 as well. 5.14 shows "in" as having a lower priority than "==", while the verbiage n 5.9 says they have the same priority.
One or the other should be corrected.
Can you please post this as a docs bug to sourceforge? Thanks!
Alex
"Alex Martelli" <al***@aleax.it> wrote in message
news:q%********************@news1.tin.it... John Roth wrote: ... You're right. Section 5.9 says this, and directly contradicts section 5.14 as well. 5.14 shows "in" as having a lower priority than "==", while the verbiage n 5.9 says they have the same priority.
One or the other should be corrected. Can you please post this as a docs bug to sourceforge? Thanks!
Done for 2.2.3. Also noted that it's probably still a problem
with 2.3.2, but I haven't checked it out.
John Roth
Alex This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Eyal |
last post by:
Hey,
I would appriciate if anyone can help on this one:
I have a java object/inteface having a method with a boolean
parameter. As I'm trying to call this method from a javascript it
fails on...
|
by: Ramprasad A Padmanabhan |
last post by:
Hello all,
On my linux box ( redhat 7.2 ), I have been using char as a boolean data
type. In order to save on the number of bytes as compared to using int
or short int.
typedef char boolean;...
|
by: Ian Tuomi |
last post by:
How can I define a boolean value in c? (an value that can only be either
1 or 0) I feel bad for the memory loss when declaring ints for variables
that do not need that much memory.
--
Ian Tuomi...
|
by: Henri |
last post by:
In java for instance there's a way to use booleans as objects and not as
value types.
I would like to do the same in VB.NET so that I can check if the boolean has
been explicitely defined (is not...
|
by: Richard Lewis Haggard |
last post by:
I'm having a problem with what appears to be some sort of confusion with
references. I have a single solution with a dozen projects which has been
working quite nicely for a while. The references...
|
by: dba123 |
last post by:
Why am I getting this error for Budget?
Error: An exception of type 'System.FormatException' occurred in
mscorlib.dll but was not handled in user code
Additional information: String was not...
|
by: KimmoA |
last post by:
First of all: I love C and think that it's beautiful. However, there is
at least one MAJOR flaw: the lack of a boolean type.
OK. Some of you might refer to C99 and its _Bool (what's up with the...
|
by: Greg Corradini |
last post by:
Hello all,
I'm having trouble understanding why the following code evaluates as it
does:
True
-1
In the 2.4 Python Reference Manual, I get the following explanation for the
'and' operator...
|
by: Flavio |
last post by:
Hi, I have been playing with set operations lately and came across a
kind of surprising result given that it is not mentioned in the
standard Python tutorial:
with python sets, intersections ...
|
by: RMWChaos |
last post by:
My brain is fried from looking at way too much code lately. So help me understand, if you will, do these operators:
if (A && B || C)
mean, "If either A and B are true or if C is true"?
And...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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: 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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |