Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does "is" work in python?

Tags:

python

module

Can please someone explain how one may use 'is' in an 'if' condition. I am working with the fractions module, and I'm having some trouble:

>>> Fraction(0, 1) is 0
False
>>> float(Fraction(0, 1))
0.0
>>> float(Fraction(0,1)) is 0.0
False

The only thing I found to work is:

>>> F = Fraction(a,b)
>>> if F >= 0:
...     if F(0, 1) <= 0:
...                      ...

Is there a way to use 'is' here? Thanks.

like image 661
milcak Avatar asked Jun 04 '11 08:06

milcak


1 Answers

a is b is equivalent to id(a) == id(b)

like image 199
saeedgnu Avatar answered Sep 17 '22 13:09

saeedgnu