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 2427
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%******** ************@ne ws1.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 a type mismatch.
It is positively because of the boolean(java primitive)parameter. It
goes fine if I change this parameter to int or String.
This inteface has a lot more methods which works fine, it is just the
|
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;
boolean x;
x=1;
|
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
Jyväskylä, Finland
"Very funny scotty, now beam down my clothes."
GCS d- s+: a--- C++>$ L+>+++$ E- W+ N+ !o>+ w---
|
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 Nothing).
But Boolean is a Structure in VB.NET (defined to False by default) and not
an Object so I'm afraid there's no way to do what I want without any
workaround.
Can you confirm that there's no object version of Boolean in VB.NET?
Thanks
|
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 between projects in the
solution were established through project references, not by browsing to an
assembly DLL. All of the projects are strongly named and use key files. Each
project's AssemblyInfo.cs specifies the assembly version, where the...
| |
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 recognized as a valid Boolean.
Public Sub UpdateCustomer_DashboardGraphs(ByVal sender As Object, ByVal
e As System.EventArgs)
|
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
uppercase 'B' anyway?) and the header you can include (apparently) to
get a real "bool". This isn't my point, however -- it should have been
there from the beginning.
char is a small int. We all know that. However, "char some_bool = 0;"
simply...
|
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 in 5.10 Boolean operations:
" The expression x and y first evaluates x; if x is false, its value is
|
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 and unions are supposed to be done
like this:
In :set('casa') & set('porca')
Out:set()
In :set('casa') | set('porca')
|
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 then this operation:
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |