To avoid the issue of whether two occurrences of the same literal are the same object (that's an implementation detail), let's give our values names so that the same name always refers to the same object.
>>> x = 5.3
>>> y = 5
We can see that in fact, when you call the float
or int
constructors on values which already are floats or ints respectively, they do not construct new objects, instead they return the original objects:
>>> x is float(x)
True
>>> y is int(y)
True
So of course float(x) is float(x)
will be True
, because that's the same as writing x is x
. Likewise for int(y) is int(y)
. The docs say "For a general Python object x
, float(x)
delegates to x.__float__()
" and "If x
defines __int__()
, int(x)
returns x.__int__()
", and it makes sense that float.__float__
and int.__int__
both just return the object itself.
However, when you call the float
constructor multiple times on the same int
value, you are constructing a new object each time:
>>> y1 = float(y)
>>> y2 = float(y)
>>> y1 is y2
False
But this is an implementation detail. The interpreter would be permitted to use a cache to return the same float
object instead of constructing multiples, and in the int
case this is what it actually does, at least for small int
values in CPython.
>>> x1 = int(x)
>>> x2 = int(x)
>>> x1 is x2
True