4 ms·
"is" isn't equivalent to equality in Python. Is specifically checks the memory address, not equality. Equality is checked with "==" like in any other language.
by c3534l 9y ago
"is" isn't equivalent to equality in Python. Is specifically checks the memory address, not equality. Equality is checked with "==" like in any other language. The author probably saw the is operator used somewhere and didn't know enough Python and figured that since && and || are reserved for bitwise operations whereas "and" and "or" are used for logical operations, "is" must also be the Python version of "==".
- thethirdone 9y agoI don't think the author meant to imply that "is" is equivalent to equality. I think the author was specifically trying to check the references to see where python stops coalescing integers.
- c3534l 9y agoI guess, but then it's also kind of uninteresting. Lists in Python are an array of pointers. That Python implements assignment differently is kind of an odd detail, but altering the value of one of the variables doesn't alter the value of the other. It's just sort of an under-the-hood implementation detail visible only through Python's introspection tools, not something that actually affects code. I tried it myself: a, b = [1, 1] a = 2 print(a is b) and got an output of false.
- yorwba 9y agoThe point is that is is making those implementation details visible. That is absolutely going to affect code relying on those details. It won't affect good code, of course, but everyone writes stupid code in a while, and if it appears to work, nobody will fix it.
- deleted 9y ago[deleted]
- Macha 9y agoJava's == is not used for equality for objects either, so it's a valid comparison because Java's behaviour is equally unintuitive here. It's the same behaviour as is for objects.
- bunderbunder 9y agoJava's behavior feels like an almost deliberate trap, though. I have previously lost hours to bugs resulting from a refactoring that changed a value's type from int to Integer, and the resulting change in equality semantics.
- mjevans 9y agoThat seems like such an anti-feature. I'd want the compiler to issue a warning for it by default and the IDE to ship with 'warnings are errors' by default.
- bunderbunder 9y agoInstead, we have some $$$$ static analysis tool that nags me endlessly about using Optional<T> private fields, but lets using the == operator for non-primitives sail past without comment.