Re:Why does `True == False is False` evaluate to False in Py...
by apricot on Saturday May 02, 2020 @11:02AM
(#23052141)
I sometimes teach programming to beginners who have some knowledge of math. A common mistake they make is to write compound conditionals like this:
if a == 2 or 3:
do stuff
because that's how they would write it in math. Does that mean Python is broken and should support that construct as well?
Re:Why does `True == False is False` evaluate to False in Py...
by edflsafoiewq on Saturday May 02, 2020 @05:14PM
(#23054970)
I believe COBOL supports this.
Re:Why does `True == False is False` evaluate to False in Py...
by maest on Saturday May 02, 2020 @06:29AM
(#23050835)
Even if Python's way was the only sane way of doing it (view which, btw, denotes having narrow experience with programming languages), that's irrelevant. GP's point was that different languages use different rules so, unless you use brackets, you won't easily know which precedence rules get applied.
Re:Why does `True == False is False` evaluate to False in Py...
by Retric on Saturday May 02, 2020 @08:10AM
(#23051269)
Python’s edge cases are just as odd as every other language
>>> a=16
>>> a*a is a*a
True
>>> a=17
>>> a*a is a*a
False
“is”, “==“, and “=“ are all very different and none of them match their mathematical equivalents very well.
Re:Why does `True == False is False` evaluate to False in Py...
by dhosek on Saturday May 02, 2020 @08:48AM
(#23051443)
Not really familiar with Python. Do you know why the second statement evaluates as false?
Re:Why does `True == False is False` evaluate to False in Py...
by mkl on Saturday May 02, 2020 @09:06AM
(#23051543)
Small integer caching: https://wsvincent.com/python-wat-integer-cache/
The objects for small integers are built-in and static, but 17^2 is too big, and so new integer objects are created when the expressions are evaluated. "is" checks if two expressions represent the same object in memory. "==" checks for equality of values.
Re:Why does `True == False is False` evaluate to False in Py...
by xen0 on Saturday May 02, 2020 @09:08AM
(#23051552)
CPython keeps an array for "small value" integers (I think in the range [-5, 256]). So when Python wants 256, it'll give you a reference to the existing object. Larger numbers require a brand new object.
Re:Why does `True == False is False` evaluate to False in Py...
by h8hawk on Saturday May 02, 2020 @09:09AM
(#23051557)
`is` is identity comparison and in python everything is "object". It check if two variable refer to same object (location in memory) or not. So you should not expect that `a is b` for `a=1.2 , b=1.2` be true. In this case actually first expression returned True is weird. because 0-256 objects are cached and every instance of them refer to same object.
Re:Why does `True == False is False` evaluate to False in Py...
by xyproto on Saturday May 02, 2020 @05:24AM
(#23050523)
Math notation is just one of many possible notations, though. And math notation is not static throughout history.
Honestly, Python's way is the only sane way to parse this. This is how a human who never programmed before with some knowledge of math would understand it.
Everyone's else mind is just corrupted by broken legacy languages.