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.
The is operator in python is used to check if two variables are pointing to the very same object and is not meant to be used to check about numeric equality. You should use == for that instead.
For example consider that
(1000 + 1000) is (1000 + 1000)
returns False.
From the documentation:
The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.
What you want here is ==, to test whether two objects are equal or equivalent by comparing the values and not the identities.
A trivial example (in CPython that may differ in other implementations):
>>> 1 + 2 == 3.0
True
>>> 1 + 2 is 3.0
False
>>> 1 + 2 is 3
True
>>> id(1 + 2)
4298185512
>>> id(3.0)
4298194656
>>> id(3)
4298185512
is checks for object identity. It returns true if two names refer to the same object. One typical usecase is to check if a name refers to None:
if foo is None:
# do stuff
a is b is equivalent to id(a) == id(b)
Quote from http://docs.python.org/library/operator.html:
operator.is_(a, b) Return a is b.
Tests object identity.
Related
Let's say I have a list with different values, like this:
[1,2,3,'b', None, False, True, 7.0]
I want to iterate over it and check that every element is not in list of some forbidden values. For example, this list is [0,0.0].
When I check if False in [0,0.0] I get True. I understand that python casts False to 0 here - but how I can avoid it and make this check right - that False value is not in [0,0.0]?
To tell the difference between False and 0 you may use is to compare them. False is a singleton value and always refers to the same object. To compare all the items in a list to make sure they are not False, try:
all(x is not False for x in a_list)
BTW, Python doesn't cast anything here: Booleans are a subclass of integers, and False is literally equal to 0, no conversion required.
You would want to use is instead of == when comparing.
y = 0
print y == False # True
print y is False # False
x = False
print x == False # True
print x is False # True
Found a weird corner case on differentiating between 0 and False today. If the initial list contains the numpy version of False (numpy.bool_(False)), the is comparisons don't work, because numpy.bool_(False) is not False.
These arise all the time in comparisons that use numpy types. For example:
>>> type(numpy.array(50)<0)
<class 'numpy.bool_'>
The easiest way would be to compare using the numpy.bool_ type: (np.array(50)<0) is (np.False_). But doing that requires a numpy dependency. The solution I came up with was to do a string comparison (working as of numpy 1.18.1):
str(numpy.bool_(False)) == str(False)
So when dealing with a list, a la #kindall it would be:
all(str(x) != str(False) for x in a_list)
Note that this test also has a problem with the string 'False'. To avoid that, you could exclude against cases where the string representation was equivalent to itself (this also dodges a numpy string array). Here's some test outputs:
>>> foo = False
>>> str(foo) != foo and str(foo) == str(False)
True
>>> foo = numpy.bool_(False)
>>> str(foo) != foo and str(foo) == str(False)
True
>>> foo = 0
>>> str(foo) != foo and str(foo) == str(False)
False
>>> foo = 'False'
>>> str(foo) != foo and str(foo) == str(False)
False
>>> foo = numpy.array('False')
>>> str(foo) != foo and str(foo) == str(False)
array(False)
I am not really an expert programmer, so there may be some limitations I've still missed, or a big reason not to do this, but it allowed me to differentiate 0 and False without needing to resort to a numpy dependency.
So I was looking at some code online and I came across a line (at line 286):
if depth > 0 and best <= -MATE_VALUE is None and nullscore > -MATE_VALUE:
The part I had trouble understanding was the best <= -MATE_VALUE is None.
So I fired up the interpreter to see how a statement such as value1 > value2 is value3 work. So I tried
>>> 5 > 2 is True
False
>>> (5 > 2) is True
True
>>> 5 > (2 is True)
True
My Question
Why is 5 > 2 is True not True? And how do these things generally work?
Thanks.
You're seeing python's operator chaining working
5 > 2 is True
Is equivalent to
5>2 and 2 is True
You can see this in that
>>> 5>2 is 2
Returns True.
First, 5 > 2 is True is equivalent to (5 > 2) and (2 is True) because of operator chaining in python (section 5.9 here).
It's clear that 5 > 2 evaluates to True. However, 2 is True will evaluate to False because it is not implicitly converted to bool. If you force the conversion, you will find that bool(2) is True yields True. Other statements such as the if-statement will do this conversion for you, so if 2: will work.
Second, there is an important difference between the is operator and the == operator (taken from here):
Use is when you want to check against an object's identity (e.g.
checking to see if var is None). Use == when you want to check
equality (e.g. Is var equal to 3?).
>> [1,2] is [1,2]
False
>> [1,2] == [1,2]
True
While this does not have an immediate impact on this example, you should keep it in mind for the future.
I've come across a surprising situation when using numpy's arrays. The following code
(True==True)+(True==True)
returns 2, as one would expect. While
import numpy
Array=numpy.zeros((2,2),dtype=bool)
(Array[0][0]==Array[0][0])+(Array[1][0]==Array[1][0])
returns True. This leads to:
(Array[0][0]==Array[0][0])+(Array[1][0]==Array[1][0])-1
returning 0, while
(Array[0][0]==Array[0][0])-1+(Array[1][0]==Array[1][0])
returns 1, making the sum not commutative!
Is this intended? If so, why?
It would appear that numpy.bool_ behaves slightly differently to vanilla Python bool:
>>> int(True+True) == int(True) + int(True)
True
>>> int(numpy.bool_(1)+numpy.bool_(1)) == int(numpy.bool_(1)) + int(numpy.bool_(1))
False
This is because:
>>> True+True
2
>>> numpy.bool_(1)+numpy.bool_(1)
True
>>> int(numpy.bool_(1)+numpy.bool_(1))
1
Basically, the addition operation for numpy.bool_ is logical, rather than numerical; to get the same behaviour with bool:
>>> int(True and True)
1
This is fine if you only use it for truthiness, as intended, but if you try to use it in an integer context without being explicit about that, you end up surprised. As soon as you're explicit, expected behaviour is restored:
>>> int(numpy.bool_(1)) + int(numpy.bool_(1))
2
I think the problem is th autocasting.
in this case:
(Array[0][0]==Array[0][0])+(Array[1][0]==Array[1][0])-1
Python do:
(Array[0][0]==Array[0][0])+(Array[1][0]==Array[1][0]) = True
True -1 =cast= 1 -1 = 0
In the second case the cast is do it before:
(Array[0][0]==Array[0][0])-1+(Array[1][0]==Array[1][0])
True - 1 + True
(True - 1 =cast= 0)
0 + True =cast again= 0+ 1 = 1
So, it's not a bug. It's a autocasting in diferent parts.
This question already has answers here:
"is" operator behaves unexpectedly with integers
(11 answers)
Closed 9 years ago.
Erratic behavior of "is" expressions in python.
>>> 258 -1 is 257
False
And
>>> 258 -1 == 257
True
How is python evaluating "is" expression ? and why does it show it as false , eventhough it is true ?
Why is it happening only to certain set of numbers ?
2 - 1 is 1
True
works perfectly fine.
is is used for identity check, to check if both variables point to the same object,
while == is used for checking values.
From the docs:
The operators is and is not test for object identity: x is y is true
if and only if x and y are the same object. x is not y yields the
inverse truth value.
>>> id(1000-1) == id(999)
False
""" What is id?
id(object) -> integer
Return the identity of an object. This is guaranteed to be unique among
simultaneously existing objects. (Hint: it's the object's memory address.)
"""
>>> 1000-1 is 999
False
>>> 1000-1 == 999
True
>>> x = [1]
>>> y = x #now y and x both point to the same object
>>> y is x
True
>>> id(y) == id(x)
True
>>> x = [1]
>>> y = [1]
>>> x == y
True
>>> x is y
False
>>> id(x),id(y) #different IDs
(161420364, 161420012)
But some small integers(-5 to 256) and small strings are cached by Python: Why (0-6) is -6 = False?
#small strings
>>> x = "foo"
>>> y = "foo"
>>> x is y
True
>>> x == y
True
#huge string
>>> x = "foo"*1000
>>> y = "foo"*1000
>>> x is y
False
>>> x==y
True
is compares object identity and yields True only if both sides are the same object. For performance reasons, Python maintains a "cache" of small integers and reuses them, so all int(1) objects are the same object. In CPython, cached integers range from -5 to 255, but this is an implementation detail, so you should not rely on it. If you want to compare equality, use ==, not is.
The is operator checks for object identity: the object created by calling 258-1 is NOT the same object as the one created by 257. The == operator checks if the values of the compared objects are the same.
Try using help('is').
The Python is operator checks for object identity, not object value. The two integers must refer to the same actual object internally for is to return true.
This will return true for "small" integers due to an internal cache maintained by Python, but two numbers with the same value will not return true if compared with is in general.
This question already has answers here:
Understanding the "is" operator [duplicate]
(11 answers)
Closed 3 years ago.
I was quite surprised when
[] is not []
evaluated to True.
What is happening in this code? What really not and is statements are doing?
a is not b is a special operator which is equivalent to not a is b.
The operator a is b returns True if a and b are bound to the same object, otherwise False. When you create two empty lists you get two different objects, so is returns False (and therefore is not returns True).
is is the identity comparison.
== is the equality comparison.
Your statement is making two different lists and checking if they are the same instance, which they are not. If you use == it will return true and because they are both empty lists.
The best way to describe WHY that happens is this:
Here is your example
>>> x = []
>>> y = []
>>> print(x is y)
... False
x and y are actually two different lists, so if you add something to x, it does not appear in y
>>> x.append(1)
>>> print(x)
... [1]
>>> print(y)
... []
So how do we make (x is y) evaluate true?
>>> x = []
>>> y = x
>>> print(x is y)
... True
>>> x.append(10)
>>> print(x)
... [10]
>>> print(y)
... [10]
>>> print(x is y)
... True
if you want to see if two lists have the same contents...
>>> x = []
>>> y = []
>>> print(x == y)
... True
>>> x.append(21)
>>> print(x)
... [21]
>>> print(y)
... []
>>> print(x == y)
... False
>>> y = [21]
>>> print(x == y)
... True
is means is same instance. It evaluates to true if the variables on either side of the operator point to the same object and false otherwise.
Reference, near the bottom.
is checks for identity. [] and [] are two different (but equivalent) lists. If you want to check if both the lists are empty you can use their truth value (false for empty strings, collections, and zeros).
if not ([] and []):
print 'Spanish Inquisition'
the only time that is is guaranteed to return True is for singletons such as None. Like the Highlander, there can be only one instance of None in your program - every time you return None it's the very same "thing" as the none referred to if you type print None.
[], OTOH, is not guaranteed to be anything except an empty list and evaluate to False in a boolean context.
I know I am posting to a pretty old post. however, this might help someone stumble upon this like me.
"is" checks, whether a memory address is same or not, while "==" check if the value is same or not. Would be much clear from the following example
let's first talk about immutable objects, as it's easy to understand
# could be any immutable object
immutable_a = 10
immutable_b = 10
# prints address of a and b variable
print "address of a is %s" % id(immutable_a)
print "address of a is %s" % id(immutable_b)
# as both addresses is same, following shall be true
print immutable_a is immutable_b
# as the values are also same, following shall be true as well
print immutable_a == immutable_b
now let's talk about mutable objects
# could be any mutable object
mutable_a = [10]
mutable_b = [10]
# prints address of a and b variable
print "address of mutable_a is %s" % id(mutable_a)
print "address of mutable_b is %s" % id(mutable_b)
# as addresses are not same, following shall be false
print mutable_a is mutable_b
# as the values are same, following shall be true
print mutable_a == mutable_b
#Jiaaro is right. Using is with immutable data types is dangerous because it is not predictable because of Pythons interpreter optimization.
See this example:
10 * "a" is 10 * "a" # True
100 * "a" is 100 * "a" # False
In the second line it is faster to create a new object with a new id for the interpreter. So use the is operator only with mutable types.