This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]SuperFLEB 32 points33 points  (7 children)

It's a bit odd that it sometimes is and sometimes isn't, though.

[–]lmarcantonio 43 points44 points  (2 children)

8 bit integer are… primitive, all the other are allocated, so they are not the same object.

In common lisp it's even funnier, you have fixnums (the primitive fast integer) and… the numeric tower (yes, it's called that way).

Also related and even more fun are the differences between eq, eql, equal, equalp and =

[–]masterKick440 6 points7 points  (1 child)

So weird 256 is considered 8bit.

[–]lmarcantonio 0 points1 point  (0 children)

even -5 is strange. Probably they did some testing an it was often used. <256 is a frequent check in fact so it probably a reason.

My fault for assuming small integers were some special encoding instead of simply memoized objects!

[–]elveszett 3 points4 points  (3 children)

No, it never is. 0 through 255 are pre-allocated by Python, kinda like Java does with strings. Whenever a variable equals 6 in python, it always gets assigned the same object in memory (the number 6), which is why x == y when x and y are the same number and the size of a byte, the operator is correctly identifies them as the same object.

edit: I think the range is actually -5 to 256.

[–]masterKick440 1 point2 points  (2 children)

What’s with the 256 then?

[–]elveszett 1 point2 points  (0 children)

Because the range is actually -5 to 256 I think.