Re:WTF Python: Exploring and understanding Python through su...
by EForEndeavour on Thursday February 11, 2021 @10:25AM
(#26102434)
Noob question: instead of using == except for True/False/None, what about just using == for all comparisons?
Re:WTF Python: Exploring and understanding Python through su...
by GoblinSlayer on Thursday February 11, 2021 @05:43AM
(#26100146)
Why that exception?
Re:WTF Python: Exploring and understanding Python through su...
by maweki on Thursday February 11, 2021 @07:09AM
(#26100641)
None, True, False are singletons and are never constructed again after initilization of the interpreter (and have been keywords in python2). That's why "is" is always safe.
Small integers, as they are kept around, probably are as well (5 is (2+3) works).
I think what gets dropped during the conversation is, that we've been living with Java doing it this way (is being == and == being .equals) for decades and nobody really complains.
Re:WTF Python: Exploring and understanding Python through su...
by GoblinSlayer on Friday February 12, 2021 @02:19AM
(#26111389)
I mean why not use == for everything?
Re:WTF Python: Exploring and understanding Python through su...
by Vaphell on Friday February 12, 2021 @10:01AM
(#26114336)
1. it's a slower check, just like equals() intended for value equality is slower than reference equality == in java.
2. 1 == True and 0 == False, as booleans subclass the int type. On the other hand is distingushes between 1 and True, and 0 and False respectively.
Rule of thumb is use == for everything except True/False/None. Once you get going with python you'll learn the meaning of is eventually, and you'll still barely ever need to use it.