Live data from Hacker News

Weird Python Integers

kate.io

121–130 of 150 posts

Re: Weird Python Integers

#122

Summary: 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.

I believe that's called "interning". It's done for strings as well.

Re: Weird Python Integers

#123

Earlier 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.

While '==' meaning compares equal by value would be nice; it makes the == operator quite complex. If it's a general comparison operator, it would need to recursively compare all the members, and avoid looping on circular references, or risk == running forever.

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

#124
post #109

Earlier quoted context omitted.

Yeah you might be right, it's hard to say. Generally I view writing as more expensive than reading so I don't know.

Note that it's not just write vs read, it's write vs read + branch.

Right, if it was just 1 read vs. 1 write then I would know!

Re: Weird Python Integers

#125

Earlier 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

No, that is not a counterexample, because while that code defines a method named "equals", the method so defined is not an equivalence relation.

Re: Weird Python Integers

#128
post #99

Earlier 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).

I think you mean reference with & (e.g. &x == &y)

Re: Weird Python Integers

#129

Earlier 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.

The point is that the language treats numbers as objects when they aren't.

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

#130

Earlier 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.

> Here a == b and no 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
Post reply on HN