I need a clarification on the semantics of Python.
Is it true that when Python encounters a statement of the form
x = some_term
a new object is being created, as specified by the process of evaluation of the term some_term
?
Now suppose that the following lines have been encountered by Python:
x = the_term
y = the_term
Where the_term
is some fixed term (assume that it does not contain any references to functions generating "random objects"). As I see this the behaviour of Python should be the following: first a new object O1 is created and associated with the name x
. Then a second new object O2 is created and associated with the name y
. Both names x
and y
are now in suitable namespace. Objects O1 and O2 are distinct objects, the are occupying different places in memory, and the got the same values. Hence the test
x == y
should return True, and test
x is y
should return False.
So tell me please why do I observe the following:
x=5
y=5
now x==y
returns (of course) True, and x is y
returns True.
But if we put:
x=99999999999999999999
y=99999999999999999999
now x==y
returns (of course) True, and x is y
returns False.
Could you tell me what is happening? What is the "real" semantics of Python (since the one I have described is clearly wrong, as these examples show)?