I'm in Python 3.6.5 and I ran across a strange phenomenon: float('50.0')
is not the same as float(50.0)
, though they are equal to each other.
I ran some code to find the difference. Other than python saying that they aren't the same, I can't find a difference. I'm baffled. I'd love it if someone could explain what is happening here.
Here are my tests:
if float('50.0') is float(50.0):
print("float('50.0') is float(50.0)")
else:
print("float('50.0') is not float(50.0)")
if float('50.0') == float(50.0):
print("float('50.0') == float(50.0)")
else:
print("float('50.0') != float(50.0)")
if float('50.0') is 50.0:
print("float('50.0') is 50.0")
else:
print("float('50.0') is not 50.0")
if float(50.0) is 50.0:
print('float(50.0) is 50.0')
else:
print('float(50.0) is not 50.0')
if float(50.0) is float(50.0):
print('float(50.0) is float(50.0)')
else:
print('float(50.0) is not float(50.0)')
xstr_float = float('50.0')
norm_float = float(50.0)
print ('xstr_float: {0:.100f}'.format(xstr_float))
print ('xstr_float is of type:{}'.format(type(xstr_float)))
print ('norm_float: {0:.100f}'.format(norm_float))
print ('norm_float is of type:{}'.format(type(norm_float)))
and my results:
float('50.0') is not float(50.0)
float('50.0') == float(50.0)
float('50.0') is not 50.0
float(50.0) is 50.0
float(50.0) is float(50.0)
xstr_float: 50.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
xstr_float is of type:<class 'float'>
norm_float: 50.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
norm_float is of type:<class 'float'>
Based on some discussion in the comments I've tried the following, getting results that don't answer the question:
x = float(50.0)
y = float(50.0)
if x is y:
print('x is y')
else:
print('x is not y')
x=12345
y=12345
if x is y:
print('x is y')
else:
print('x is not y')
results in:
x is y
x is y
UPDATE: I've marked an answer that is correct, and based on a comment on that answer I want to show other people who might be confused what I was missing:
When python creates an object it is assigned an ID. is
returns true when comparing two objects returns the same ID. Sometimes python will cache and reuse an object. So in the case of x is y
we can see that an object is given and reused because the ID is the same. We also see that the ID changes between python sessions:
x=12345
y=12345
if x is y:
print('x is y')
else:
print('x is not y')
print('x_id: {}'.format(id(x)))
print('y_id: {}'.format(id(y)))
results in
x is y
x_id: 2476500365040
y_id: 2476500365040
and on the next run it results in
x is y
x_id: 2234418638576
y_id: 2234418638576
If for any reason the same object couldn't be reused to represent x and y then x is y
would return false.