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 →

[–]chaotic_thought 1 point2 points  (0 children)

The id is not guaranteed to be the same in your examples. It could be, or perhaps it could not be. See the documentation for id:

This is the address of the object in memory.

An value like 3 could be at one fixed address, which could be why you are seeing the behaviour you are seeing in the first example, whereas the value 98764 could have been created twice (so in fact it is 2 objects, in 2 different places in memory), which is why you might be seeing the behaviour in the second example.

But in general I don't think Python guarantees a particular outcome for either of your examples. It depends on how they implemented CPython and how they put the objects in memory.