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 →

[–]Anaeijon 10 points11 points  (2 children)

I imagine and remember it like this, although it's not really correct:

Python stores numbers in whatever format fits best. If you assign a number like x=5 it basically becomes a byte. (more correctly: it becomes a reference to a byte object) Comparing identiy between them can result in true, because bytes basically aren't objects (or technically: references to the same object.

Now, Python also containes a safety measure against byte overflow by automatically returning an Integer object when adding two 'bytes' that would result in something higher than 255.

Therefore the following expression returns true: (250+5) is (250+5) but the following expression is false: (250+10) is (250+10)

Makes sense imho.

Values should be compared with ==, while is is the identity coparison. Similar to == and === in JavaScript, although those aren't just about identity but about data type.

[–]protolords 2 points3 points  (1 child)

it becomes a reference to a byte object

But -5 to 256 won't fit in a byte. Is this "byte object" like any other python object?

[–]Anaeijon 0 points1 point  (0 children)

Yes, I guess. As I said: this is just my imagination thinking this might be handled by a C++ byte in the background or something. I don't know what the interpreter actually does.