Jacques Gaudin
Jacques Gaudin

Reputation: 16958

Why does float('Inf') is float('Inf') returns false in Python?

Whilst reading about Python's infinity, I came accross this :

>>>float('Inf') == float('Inf')
True
>>>float('Inf') is float('Inf')
False
>>>float('Inf') is not float('Inf')
True

I understand that equals works but I am wondering what float('Inf') actually points to that make the is test return False ? Is it different each time you call float('Inf') ?

EDIT: I am not asking about the difference between == and is. I am asking about the implementation details of float('Inf') and the nature of this object.

Upvotes: 0

Views: 3079

Answers (4)

ViennaMike
ViennaMike

Reputation: 2337

And you don't need to invoke float() or 'Inf' to see this:

a = 1. 
b = 1. 
a == b 
True 
a is b 
False

Adding as answer only because I can't get formatting to work properly as a comment to @Nishant response.

Upvotes: 0

zephyr
zephyr

Reputation: 2332

When you say float('Inf'), you're creating a new object on the fly that gets stored in memory somewhere and has a reference to that memory location. When you do

>>>float('Inf') == float('Inf')

You're creating two such objects, each with the same value, but in two different memory locations. The comparison here will compare the two values in these two memory locations and of course it will come out true.

When you do

>>>float('Inf') is float('Inf')

again you're creating two new objects each of the same value, but in different locations. But now you're trying to compare the references to the memory locations and of course those are not going to be the same.

This may be more clear if you consider the concept of

>>>a = float('Inf')
>>>b = float('Inf')
>>>a == b
True
>>>a is b
False

Hopefully, once you consider that a and b are the actual objects you're creating (rather than creating them with no name as in your examples) it is more clear what is happening.

Upvotes: 0

Moses Koledoye
Moses Koledoye

Reputation: 78546

is and == in python are quite different. is checks for identity while == checks for equality as some other users have already stated here:Is there a difference between == and is in Python?

float() creates (or might create, depending on your interpreter) a new object which is not identical (but is equal) to another object created by float() having the same argument.

Cpython may return a True for your check, but other VMs or interpreters may not as described by Microsoft

So:

 >>> float(2) is float(2)
 >>> False
 >>> float(2) == float(2)
 >>> True

In fact their identities can be seen as completely different using id()

 >>> id(float(2))
 >>> 251452
 >>> id(float(2)) # same call
 >>> 251934       # different result

So you should only use is if you really, really want to test for identity and not value.

Upvotes: 2

Nishant Singh
Nishant Singh

Reputation: 3209

Simply put, is works on objects and will only return TRUE if and if the 2 variables are pointing to same object , while == works on value so,, the reason it says False is because a new float is called up

Upvotes: 0

Related Questions