A question about identity and boolean in Python3
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Horror Game Menu Looping
--
Chapters
00:00 A Question About Identity And Boolean In Python3
00:24 Answer 1 Score 2
00:59 Accepted Answer Score 3
01:35 Thank you
--
Full question
https://stackoverflow.com/questions/5830...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #boolean #identity
#avk47
ACCEPTED ANSWER
Score 3
Python chains comparisons:
Formally, if
a, b, c, …, y, zare expressions andop1, op2, …, opNare comparison operators, thena op1 b op2 c ... y opN zis equivalent toa op1 b and b op2 c and ... y opN z, except that each expression is evaluated at most once.
Your expression is:
False is not True is not True is not False is not True
Which becomes:
(False is not True) and (True is not True) and (True is not False) and (False is not True)
Which is equivalent to:
(True) and (False) and (True) and (True)
Which is False.
ANSWER 2
Score 2
is relates to identity.
When you ask if x is y, you're really asking are x and y the same object? (Note that this is a different question than do x and y have the same value?)
Likewise when you ask if x is not y, you're really asking are x and y different objects?
Specifically in regards to True and False, Python treats those as singletons, which means that there is only ever one False object in an entire program. Anytime you assign somnething to False, that is a reference to the single False object, and so all False objects have the same identity.