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

Tell me the truth


In the python-ideas mailing list
http://mail.python.org/pipermail/pyt...ch/thread.html there
was a discussion about the fact that python has opeartors 'and', 'or' and
'not' (keywords of the language) but 'bool' is a type. Naturally we have
(not not x) == bool(x) # always True
If I take into account the fact that 'True' and 'False' are singletons
(guaranteed ?) :
(not not x) is bool(x) # should be always True.
>>def ok(x):
.... return (not not x) is bool(x)
....
>>ok(0)
True
>>ok(False)
True

ok(11) is True
True
Interesting.... Let's make another function:
>>def foo(x):
.... return (not not x) is bool(x) is True
....
>>foo(False)
False
>>foo(0.0)
False
>>foo(0.1)
True

To see if the problem comes from using the a is b is True form, let's try
>>def ok3(x):
.... return (not not x) is bool(x) == True
....
>>ok3(44)
True
>>ok3(0)
False

After much head scrating and experimenting with dis.dis() I have found that
chaining comparisons (with is or ==) a == b == c in Pythonis never a good
idea. It is interpreted as
( a == b ) and ( b == c)
Such a magic is fine when we write:
if 0.0 <= x < 1.0:
but it renders the outcome of if a == b == c: somewhat confusing. In the
reference documentation the sentences """Comparisons can be chained
arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except
that y is evaluated only once (but in both cases z is not evaluated at all
when x < y is found to be false). """ should be followed by a warning about
chaining '==' or similar operators.

Regards

PS. Sorry for bad english.
NOTICE: This message contains information which is confidential and the
copyright of our company or a third party. If you are not the intended
recipient of this message please delete it and destroy all copies. If
you
are the intended recipient of this message you should not disclose or
distribute this message to third parties without the consent of our
company. Our company does not represent, warrant and/or guarantee that
the integrity of this message has been maintained nor that the
communication is free of virus, interception or interference. The
liability of our company is limited by our General Conditions of
Services.
Nota : Ce message contient des informations confidentielles propriété de
notre société et/ou d'un tiers. Si vous n'êtes pas parmi les
destinataires désignés de ce message, merci de l'effacer ainsi que
toutes ses copies. Si vous êtes parmi les destinataires désignés de ce
message, prière de ne pas le divulguer ni de le transmettre à des tiers
sans l'accord de notre société. Notre société ne peut garantir que
l'intégrité de ce message a été préservée ni que la présente
communication est sans virus, interception ou interférence. La
responsabilité de notre société est limitée par nos Conditions Générales
de Services.
Mar 8 '07 #1
2 1074
fr****************@bureauveritas.com wrote:
After much head scrating and experimenting with dis.dis() I have found
that chaining comparisons (with is or ==) a == b == c in Python is
never a good idea. It is interpreted as
( a == b ) and ( b == c)
Such a magic is fine when we write:
if 0.0 <= x < 1.0:
but it renders the outcome of if a == b == c: somewhat confusing.
I don't understand why it is confusing. What other meaning would you
expect?
Mar 8 '07 #2

"Duncan Booth" <du**********@invalid.invalidwrote in message
news:Xn*************************@127.0.0.1...
fr****************@bureauveritas.com wrote:
After much head scrating and experimenting with dis.dis() I have found
that chaining comparisons (with is or ==) a == b == c in Python is
never a good idea. It is interpreted as
( a == b ) and ( b == c)
Such a magic is fine when we write:
if 0.0 <= x < 1.0:
but it renders the outcome of if a == b == c: somewhat confusing.

I don't understand why it is confusing. What other meaning would you
expect?
I can see how one might get themselves confused. I think it simply boils
down to thinking that

A == B == C is shorthand for: (A == B) == C

It's not. You're asserting that A is equivalent to B and B is equivalent to
C, NOT that the value of comparing A to B is equivalent to C:
>>False is False
True
>>False is False is True
False

If what you want is the second form, a pair of parenthesis forcing order of
evaluation will give it to you.
>>(False is False) is True
True
-ej
Mar 8 '07 #3

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

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
6
by: lkrubner | last post by:
I've got a site on a hosted virtual server. I'd like to know the truth of what is going on, but I can't get past my browsers cache. There is some kind of error in my code that occurs on a given...
12
by: vinayakapuram | last post by:
program1 :1 main() { printf("trip" "trapas"); } output:trip traps prog 2:
4
by: James Stroud | last post by:
Hello All, I'm curious, in py0 | (1 == 1) 1 pyFalse | (1 == 1) True What is the logic of the former expression not evaluating to True (or
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
0
by: 2Barter.net | last post by:
" Given BACK what was freely GIVEN " More options 2 messages - Expand all 2Barter.net View profile More options Dec 12, 9:48 pm Blessing Are Country
2
by: lemnitzer | last post by:
TV report: all 15 British sailors confess to illegally entering Iranian Waters Here are the links: http://news.google.com/news?hl=en&ned=us&q=all+15&btnG=Search+News...
6
by: natkw1 | last post by:
Hi all, I'm new to C and kind of confused on this. I've had a look around this group for suggestions but still not sure why the warning is occurring. What I've done is this <code snip> void...
6
by: ambanks04 | last post by:
ok taking computer science class anybody know what to do i am not computer literate CoSc 111.003 Spring 2008 Assignment 4 1) Each student shall design, develop, code, test and submit an...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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...
0
Oralloy
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 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.