Weird Python Integers
121–130 of 150 posts
Re: Weird Python Integers
#122Summary: Integers in python are full blown objects. Small numbers are stored in a central preallocated table where each entry represents one number. Setting a variable to a small integer makes it point to an entry in that table. Multiple variables may point to the same small integer objects in that table. Fooling around with the table leads to funny results.
Re: Weird Python Integers
#123Earlier quoted context omitted.
On the off-chance that you're serious, I'll point out that there is no way to compare equal by identity without also comparing equal by value.
Can you elaborate on this? Integer a = new Integer(2); Integer b = a; Here a == b and no comparison by value. Integer c = new Integer(2); Then a != c but a.equals(c) (in Java). My point is that I do not agree with that and would like to have a == c, which would be the case if '==' was implemented using comparison by value.
Certainly, one can come up with examples of how a different operator == would be useful for certain types of objects (including Integer), but the architects of Java strongly felt that an operator should always behave the same, regardless of the type of its operands, unless the operator is + and the objects are Strings.
Re: Weird Python Integers
#124Re: Weird Python Integers
#125Earlier quoted context omitted.
On the off-chance that you're serious, I'll point out that there is no way to compare equal by identity without also comparing equal by value.
I don't know Java so I may have missed something, but isn't this[0] a counter-example? [0]: https://ideone.com/w8NhmR
Re: Weird Python Integers
#126Yup, 0day hunters have fun with this behavior from time to time.
> has 4-byte arbitrary global write primitive
> uses it to change arithmetic
Re: Weird Python Integers
#127Re: Weird Python Integers
#128Earlier quoted context omitted.
C++ uses `==` for whatever you want. But generally in C++, user-defined types will be documented as either having 'reference semantics' or 'value semantics' and all aspects of the type will conform to that.
In C++, == pretty much always uses value semantics regardless, because reference semantics are pretty much always explicit (dereference with star etc).
Re: Weird Python Integers
#129Earlier quoted context omitted.
What's the meaning of new Long(5) == new Long(5);? I know why the implementation lets me ask that question and I know why it's false, but I'd say that there's no reasonable question I'd ever want to ask using that expression.
You can think up an unreasonable example for anything though. If I have a graph data structure then `node == node` with identity does make sense to me in lots of cases. And what's the value equality of `(new Object()).equals(new Object())` - they have no value.
It's not that reference semantics don't have a place, it's just that the place isn't numbers.
As for your second question, it looks like the type system has two embarrassing questions: what the heck does "new Object()" mean? Nothing worth saying.
Re: Weird Python Integers
#130Earlier quoted context omitted.
On the off-chance that you're serious, I'll point out that there is no way to compare equal by identity without also comparing equal by value.
Can you elaborate on this? Integer a = new Integer(2); Integer b = a; Here a == b and no comparison by value. Integer c = new Integer(2); Then a != c but a.equals(c) (in Java). My point is that I do not agree with that and would like to have a == c, which would be the case if '==' was implemented using comparison by value.
Um, nonsense?
class Intbox {
public static void main(String[] args) {
Integer a = new Integer(2);
Integer b = a;
System.out.println(a == b);
System.out.println(a.equals(b));
}
}
$ java Intbox
true
true