News for hackers. Stuff that matters.
Re:Why does `True == False is False` evaluate to False in Py... by arcticbull on Saturday May 02, 2020 @03:17AM (#23050002)
Sounds like “is” is lower precedence than ==. Nothing to see here folks, move along?
Re:Why does `True == False is False` evaluate to False in Py... by tromp on Saturday May 02, 2020 @03:25AM (#23050049)
No; it's not about precedence at all. It's about semantics of chained comparisons, which is not what you'd expect in Python.
Re:Why does `True == False is False` evaluate to False in Py... by aktuel on Saturday May 02, 2020 @03:32AM (#23050086)
The syntax works exactly like it's supposed to. "is" is an alias for "==". This kind of chained comparison makes code much more readable. This contrived example which I never encountered as a practical issue not withstanding.
Re:Why does `True == False is False` evaluate to False in Py... by scaredginger on Saturday May 02, 2020 @03:37AM (#23050111)
I think you need to brush up on Python...

>>> x = 25 * 37

>>> y = 25 * 37

>>> x is y

False

>>> x == y

True

From my understanding, '==' checks for equality and 'is' checks that the variables reference the same address.

Re:Why does `True == False is False` evaluate to False in Py... by aktuel on Saturday May 02, 2020 @06:12AM (#23050741)
OK this is truly weird:

  >>> a=16
  >>> a*a is a*a
  True
  >>> a=17
  >>> a*a is a*a
  False
WTF
Re:Why does `True == False is False` evaluate to False in Py... by chrismorgan on Saturday May 02, 2020 @06:18AM (#23050769)
CPython interns the integers between -5 and 256.

See https://stackoverflow.com/questions/306313/is-operator-behav... for some discussion and further related links.

Re:Why does `True == False is False` evaluate to False in Py... by amelius on Saturday May 02, 2020 @06:38AM (#23050885)
I guess it's ok if you are aware of it but this can be a source of bugs: let's say you're not aware of this "feature" and you are in the middle of writing some code and want to know whether A is A always evaluates to true for integers. So you fire up Python and type "10 is 10" which gives True. ...
Re:Why does `True == False is False` evaluate to False in Py... by Alphaeus on Sunday May 03, 2020 @06:54PM (#23062565)
Using 'is' on integers raises a SyntaxWarning in 3.8+
Re:Why does `True == False is False` evaluate to False in Py... by lonelappde on Saturday May 02, 2020 @09:25AM (#23051641)
That's why you should read the spec and not guess a function's definition from one example.

Otherwise you'll end up thinking that almost everything is simply an alias for constant True or 0 or Error, by popping in 0 to any operator you check.

Re:Why does `True == False is False` evaluate to False in Py... by amelius on Saturday May 02, 2020 @09:45AM (#23051739)
No. Imho, a good language designer will make it possible to learn the language incrementally without surprises. For example, a tutorial on a language should not start with "please read the spec first or you may encounter some awkward and counter-intuitive behavior in areas you thought you had already mastered".
Re:Why does `True == False is False` evaluate to False in Py... by dragonwriter on Sunday May 03, 2020 @07:01PM (#23062601)
At the Python (rather than CPython implementation) level, the explanation is “is is object identity not value equality, and there is no guarantee that equal integers share object identity.”

At the CPython level, you can explain it in terms of the particular range of small integers that are interned and thus are guaranteed within particular CPython versions to share object identity when they have value equality.

But just knowing that is identity and == is equality is mostly enough to use them correctly.

Re:Why does `True == False is False` evaluate to False in Py... by lonelappde on Saturday May 02, 2020 @09:23AM (#23051628)
Maybe also delete or reply to your previous commented in which you made strong assertions of wrong facts.
Re:Why does `True == False is False` evaluate to False in Py... by Igelau on Saturday May 02, 2020 @08:18AM (#23051300)
Maybe you should stick to assembly?
Re:Why does `True == False is False` evaluate to False in Py... by wrboyce on Saturday May 02, 2020 @03:35AM (#23050105)
“==“ is not an alias of “is”, they behave quite differently.
Re:Why does `True == False is False` evaluate to False in Py... by 101404 on Saturday May 02, 2020 @05:32AM (#23050553)
`x is y` is an alias for `id(x) == id(y)`.
Re:Why does `True == False is False` evaluate to False in Py... by sbergjohansen on Saturday May 02, 2020 @04:41AM (#23050360)
To see that this is not about precedence, evaluate the following:

  >>> True == False is False
  False
  >>> False == True is False
  False
Re:Why does `True == False is False` evaluate to False in Py... by cjhopman on Saturday May 02, 2020 @07:26AM (#23051092)
Or think about it for a moment and realize that it would be True with either precedence if interpreted as expected.

Or read the very first part of the linked page that shows:

  >>> (True == False) is False
  True
  >>> True == (False is False)
  True
Re:Why does `True == False is False` evaluate to False in Py... by collyw on Saturday May 02, 2020 @09:06AM (#23051542)
Would I be correct in avoiding "is" for any simple data types, and only using it for objects?
Re:Why does `True == False is False` evaluate to False in Py... by lonelappde on Saturday May 02, 2020 @09:27AM (#23051654)
Integers are not simple data types in python. https://docs.python.org/2/c-api/int.html#PyInt_FromLong

You'd be correct to use 'is' to mean 'is', and '==' to mean '==', following their definitions.

Re:Why does `True == False is False` evaluate to False in Py... by Scarblac on Saturday May 02, 2020 @10:28AM (#23051976)
It's idiomatic to use it for comparisons with the value `None`, which is a singleton so it's always that one.

Other than that, just don't use `is`.

Re:Why does `True == False is False` evaluate to False in Py... by sbergjohansen on Saturday May 02, 2020 @09:43AM (#23051733)
See the comment by lonelappde, but note that the example under discussion here is actually not about `is` vs. `==` either.
Re:Why does `True == False is False` evaluate to False in Py... by sbergjohansen on Saturday May 02, 2020 @08:23AM (#23051329)
Yes. By the time I realised my second example is redundant, it was already too late to edit the comment. Thanks for clarifying.