4 ms·
It's just a operator precedence problem. Add parentheses and it goes away. Python 3.6.9 (default, Apr 18 2020, 01:56:04) >>> True == False is False
by Animats 7y ago
It's just a operator precedence problem. Add parentheses and it goes away.
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
>>> True == False is False
False
>>> (True == False) is False
True
There are worse problems with Python's "is".
>>> 1+1 is 2
True
>>> 1000+1000 is 2000
False
This comes from a bad idea borrowed from LISP. Numbers are boxed, and the small integers have boxes built in for them. Larger numbers have boxes dynamically generated. In Python "is" means "in the same box". This corresponds to (eq a b) in LISP.[1] Exposing the implementation like that might have been a good idea when McCarthy came up with it in 1960.
[1] http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node74.html http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node74.html
- hombre_fatal 7y agoIt's not demonstrating an operator precedence problem because you still aren't explaining why `True == False is False` returns False and that was the question. In fact, a problem is that it looks like an operator precedence issue at first glance. Of course, TFA explains the answer.
- bostonpete 7y agoHow is that operator precedence? Putting the parens around "False is False" also makes the expression True. The reason (chained comparison) is explained in the link and I don't think it amounts to operator precedence....(?)
- gpm 7y agoIt's not operator precedence, both (True == False) is False and True == (False is False) are true. As explained in the link, it's because of chained comparisons, it expands to True == False and False is False. More useful for expressions like 1 < x < 3.
- kazinator 7y agoI don't think that different equalities like "==" and "is" should be chained together; that is completely wrong. If it must be done for consistency in syntax/parsing, then such combinations should be semantically diagnosed and rejected. "x is y is z" -> good. "x == y == z" -> good. "x == y is z" -> WTF, error. All the operators in the "relational cluster" should belong to the same equivalence family.
- ladberg 7y agoFYI, it seems more complicated than just precedence: >>> True == False is False False >>> True == (False is False) True >>> (True == False) is False True
- taeric 7y agoYou beat me to it. :) At first glance this looks like an easy problem. But actually building the different parse trees for what I would expect here, and in all cases it should be True.
- kazinator 7y agoFor the unparenthesized case, it must be building a parse tree that is different from the parenthesized cases, namely a ternary parse tree node for an imaginary ==is op with screwy semantics. So that is to say: a == (b is c) --> == / \ a is / \ b c (a == b) is c --> is / \ == c / \ a b a == b is c --> ==_is / | \ a b c Assuming that the == and is are operator tokens and a, b, c are operands, that seems to be one rational hypothesis. Another hypothesis is that == and is are not pure functions that operate on values, but are special operators that operate on syntax, and treat a parenthesized expression differently from an unparenthesized one. (In what way does this sort of thing belong in a self-proclaimed newbie-friendly language? This has to be a bug.)
- taeric 7y agoI'm convinced it is a newbie friendly language only because they are so vehement in claiming that.
- kazinator 7y agoMulti-way equivalence is more useful if it has OR semantics rather than AND. If you're working in TXR Lisp, you can use the meq, meql and mequal functions for three or more argument equality. The call: (meq a b c ...) does not mean anything similar to: (and (eq a b) (eq b c) ...) but the semantics (except for operand evaluation) is like: (or (eq a b) (eq a c) (eq a d) ...) and likewise for the other two. It's testing whether the left argument is equal to at least one of the remaining arguments. These functions can be called with only one argument, in which case they yield false, just like (or). These functions are very useful in cond statements that include some tests that prevent conversion into caseq/caseql/casequal. (cond ((meql x 1 2 3) ... ) ;; x is one of 1 2 3 ((> x 10) ...) ;; x is greater than 10 (t ...)) ;; otherwise m stands for "multi"; or, if you like, it stands for "member" because (meql x a b c) replaces (memql x (list a b c)).
- bl0b 7y agoHad to test it out myself.. seems like the odd boxing behavior is gone in 3.7.4. But present in 3.6.6.
- matheusmoreira 7y agoSo all Python numbers are objects and the implementation automatically pre-allocates and reuses instances for small numbers? What a strange implementation. I thought it used tagged pointers like other dynamic languages. In Ruby, values can be pointers to objects but also 63 bit numbers when the least significant bit is set. https://en.wikipedia.org/wiki/Tagged_pointer https://en.wikipedia.org/wiki/Tagged_pointer This issue of object identity and equality isn't unique to Lisp but I agree that applying it to numbers produces unexpected results.
- pansa2 7y ago> I thought it used tagged pointers like other dynamic languages. No. IIRC tagged pointers were considered too complex to expose via Python’s C API.
- matheusmoreira 7y agoIn Ruby C extensions it works like this: int FIXNUM_P(VALUE object_pointer) { return object_pointer & 1; } long FIX2LONG(VALUE object_pointer) { return object_pointer >> 1; } VALUE ruby_value; long number; if (FIXNUM_P(ruby_value)) number = FIX2LONG(ruby_value); I thought this was pretty simple. What does Python's C API look like?
- __s 7y agohttps://mail.python.org/pipermail/python-dev/2004-July/046139.html https://mail.python.org/pipermail/python-dev/2004-July/04613... -1000 from GvR
- userbinator 7y agoHere's his actual reply: https://mail.python.org/pipermail/python-dev/2004-July/046147.html https://mail.python.org/pipermail/python-dev/2004-July/04614...
- jraph 7y agoJava does the same thing for small numbers.
- deleted 7y ago[deleted]
- c3534l 7y agoI don't think it's that bad. "is" shouldn't be used for equality like that and most people won't think to because it's weird. Maybe they should have named it __is__ so people know it's a scary operation that might confuse beginners.
- lispm 7y agoEQ in Lisp means 'same object'.
- kazinator 7y agoeq may expose implementation (it's even called "implementation equality"), but it's very useful. Implementation equality lets you use an object as a key in a lookup mechanism which adds external associations with an object (which could be de facto properties of that object, or links to other objects). You cannot do that with an equality that deems similar objects to be equal. eq does not mean "in the same box". In many implementations, it means "same bit pattern". Two unboxed integer objects (fixnums) are eq if they are the same integer (bit pattern). However the ANSI Common Lisp standard doesn't require small integers to be eq to themselves. eql is used for testing for "same number, and eq equality for everything else", which means that 95% of the time you want eql if you think you want eq. The 5% you're sure you're only comparing symbols, or else objects such as structures or CLOS objects for their identity.