By Anisuzzaman Khan


2012-11-30 17:39:14 8 Comments

The is operator does not match the values of the variables, but the instances themselves.

What does it really mean?

I declared two variables named x and y assigning the same values in both variables, but it returns false when I use the is operator.

I need a clarification. Here is my code.

x = [1, 2, 3]
y = [1, 2, 3]

print x is y #It prints false!

10 comments

@Martijn Pieters 2012-11-30 17:40:23

You misunderstood what the is operator tests. It tests if two variables point the same object, not if two variables have the same value.

From the documentation for the is operator:

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.

Use the == operator instead:

print x == y

This prints True. x and y are two separate lists:

x[0] = 4
print(y)  # prints [1, 2, 3]
print(x == y)   # prints False

If you use the id() function you'll see that x and y have different identifiers:

>>> id(x)
4401064560
>>> id(y)
4401098192

but if you were to assign y to x then both point to the same object:

>>> x = y
>>> id(x)
4401064560
>>> id(y)
4401064560
>>> x is y
True

and is shows both are the same object, it returns True.

Remember that in Python, names are just labels referencing values; you can have multiple names point to the same object. is tells you if two names point to one and the same object. == tells you if two names refer to objects that have the same value.

@imallett 2016-07-12 22:29:45

So, A is B is the same as id(A) == id(B).

@Martijn Pieters 2016-07-13 10:39:21

@imallett: that's a proxy for the same test, provided you don't store id(A) in a variable and later expect variable == id(B) to still work; if A was deleted in the meantime then B could have been given the same memory location.

@imallett 2016-07-13 21:19:28

Makes sense, and it's also the Right Thing; variable is storing a property of something that formerly existed. There's no way for the runtime to detect that the later usage is erroneous. The key part of the standard is "[id() ]is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value."

@Haranadh 2017-06-06 05:27:04

Could not format in comments. But there is an interesting thing. :) >>> x = 5 \n >>> y = 5 \n >>> x is y \n True \n >>> x == y \n True \n >>> \n

@Magnus Lyckå 2017-09-21 13:56:37

Small integers are interned in CPython since they are used so often. It's an optimization. x=5;y=5;x is y => True because id(x) == id(y). It's the same integer object which is reused. Works in Python since integers are immutable. If you do x=1.0; y=1.0 or x=9999;y=9999, it won't be the same identity, because floats and larger ints aren't interned.

@Martijn Pieters 2017-09-21 14:43:51

@MagnusLyckå there are other optimisations that can cause immutable objects to be cached. For example, if you run your example in a new function or together with a separating semicolon in the interactive interpreter, you'd find they have the same id too.

@Magnus Lyckå 2017-09-26 19:29:41

Which means my compact cases above are actually wrong! Thanks for pointing that out @martijn-pieters ! I should know by now never to post code without testing it! >>> x=1.0; y=1.0 >>> x is y True >>> x=1.0 >>> y=1.0 >>> x is y False

@jbrezik 2019-02-07 19:10:32

Thank you for pointing out the id() function, something that is not mentioned in a lot of tutorials. Definitely neat way to debug objects.

@ForsakenOne 2018-10-29 19:34:51

A simple example with fruits

fruitlist = [" apple ", " banana ", " cherry ", " durian "]
newfruitlist = fruitlist
verynewfruitlist = fruitlist [:]
print ( fruitlist is newfruitlist )
print ( fruitlist is verynewfruitlist )
print ( newfruitlist is verynewfruitlist )

Output:

True
False
False

If you try

fruitlist = [" apple ", " banana ", " cherry ", " durian "]
newfruitlist = fruitlist
verynewfruitlist = fruitlist [:]
print ( fruitlist == newfruitlist )
print ( fruitlist == verynewfruitlist )
print ( newfruitlist == verynewfruitlist )

The output is different:

True
True
True

That's because the == operator compares just the content of the variable. To compare the identities of 2 variable use the is operator

To print the identification number:

print ( id( variable ) )

@Nizam Mohamed 2016-07-29 11:59:35

x is y is same as id(x) == id(y), comparing identity of objects.

As @tomasz-kurgan pointed out in the comment below is operator behaves unusually with certain objects.

E.g.

>>> class A(object):
...   def foo(self):
...     pass
... 
>>> a = A()
>>> a.foo is a.foo
False
>>> id(a.foo) == id(a.foo)
True

Ref;
https://docs.python.org/2/reference/expressions.html#is-not
https://docs.python.org/2/reference/expressions.html#id24

@Tomasz Kurgan 2018-06-26 11:23:25

Not, it doesn't. It might behave similar in most cases, but it's not always true. See this - the very bottom of the page, bullet 6.: > (...), you may notice seemingly unusual behaviour in certain uses of the is operator, like those involving comparisons between instance methods, or constants And the minimal working example: ` class A(object): def foo(self): pass a = A() print a.foo is a.foo print id(a.foo) == id(a.foo) `

@Nizam Mohamed 2018-06-29 18:48:13

@tomasz-kurgan Thank you

@gixxer 2015-12-03 01:42:55

is and is not are the two identity operators in Python. is operator does not compare the values of the variables, but compares the identities of the variables. Consider this:

>>> a = [1,2,3]
>>> b = [1,2,3]
>>> hex(id(a))
'0x1079b1440'
>>> hex(id(b))
'0x107960878'
>>> a is b
False
>>> a == b
True
>>>

The above example shows you that the identity (can also be the memory address in Cpython) is different for both a and b (even though their values are the same). That is why when you say a is b it returns false due to the mismatch in the identities of both the operands. However when you say a == b, it returns true because the == operation only verifies if both the operands have the same value assigned to them.

Interesting example (for the extra grade):

>>> del a
>>> del b
>>> a = 132
>>> b = 132
>>> hex(id(a))
'0x7faa2b609738'
>>> hex(id(b))
'0x7faa2b609738'
>>> a is b
True
>>> a == b
True
>>>

In the above example, even though a and b are two different variables, a is b returned True. This is because the type of a is int which is an immutable object. So python (I guess to save memory) allocated the same object to b when it was created with the same value. So in this case, the identities of the variables matched and a is b turned out to be True.

This will apply for all immutable objects:

>>> del a
>>> del b
>>> a = "asd"
>>> b = "asd"
>>> hex(id(a))
'0x1079b05a8'
>>> hex(id(b))
'0x1079b05a8'
>>> a is b
True
>>> a == b
True
>>>

Hope that helps.

@Haranadh 2017-06-06 05:37:59

this is real nice example. thanks for detailed info.

@user2183078 2017-10-05 17:29:09

But try a=123456789 b=123456789

@smart 2018-10-14 14:01:18

Everything less than -5 or higher than 256 in Python will be False. Python caches numbers in range [-5, 256].

@abarnert 2014-09-10 05:38:57

Another duplicate was asking why two equal strings are generally not identical, which isn't really answered here:

>>> x = 'a' 
>>> x += 'bc'
>>> y = 'abc'
>>> x == y
True
>>> x is y
False

So, why aren't they the same string? Especially given this:

>>> z = 'abc'
>>> w = 'abc'
>>> z is w
True

Let's put off the second part for a bit. How could the first one be true?

The interpreter would have to have an "interning table", a table mapping string values to string objects, so every time you try to create a new string with the contents 'abc', you get back the same object. Wikipedia has a more detailed discussion on how interning works.

And Python has a string interning table; you can manually intern strings with the sys.intern method.

In fact, Python is allowed to automatically intern any immutable types, but not required to do so. Different implementations will intern different values.

CPython (the implementation you're using if you don't know which implementation you're using) auto-interns small integers and some special singletons like False, but not strings (or large integers, or small tuples, or anything else). You can see this pretty easily:

>>> a = 0
>>> a += 1
>>> b = 1
>>> a is b
True
>>> a = False
>>> a = not a
>>> b = True
a is b
True
>>> a = 1000
>>> a += 1
>>> b = 1001
>>> a is b
False

OK, but why were z and w identical?

That's not the interpreter automatically interning, that's the compiler folding values.

If the same compile-time string appears twice in the same module (what exactly this means is hard to define—it's not the same thing as a string literal, because r'abc', 'abc', and 'a' 'b' 'c' are all different literals but the same string—but easy to understand intuitively), the compiler will only create one instance of the string, with two references.

In fact, the compiler can go even farther: 'ab' + 'c' can be converted to 'abc' by the optimizer, in which case it can be folded together with an 'abc' constant in the same module.

Again, this is something Python is allowed but not required to do. But in this case, CPython always folds small strings (and also, e.g., small tuples). (Although the interactive interpreter's statement-by-statement compiler doesn't run the same optimization as the module-at-a-time compiler, so you won't see exactly the same results interactively.)


So, what should you do about this as a programmer?

Well… nothing. You almost never have any reason to care if two immutable values are identical. If you want to know when you can use a is b instead of a == b, you're asking the wrong question. Just always use a == b except in two cases:

  • For more readable comparisons to the singleton values like x is None.
  • For mutable values, when you need to know whether mutating x will affect the y.

@Amadan 2014-09-10 04:56:26

Prompted by a duplicate question, this analogy might work:

# - Darling, I want some pudding!
# - There is some in the fridge.

pudding_to_eat = fridge_pudding
pudding_to_eat is fridge_pudding
# => True

# - Honey, what's with all the dirty dishes?
# - I wanted to eat pudding so I made some. Sorry about the mess, Darling.
# - But there was already some in the fridge.

pudding_to_eat = make_pudding(ingredients)
pudding_to_eat is fridge_pudding
# => False

@Tom Close 2015-10-01 06:47:47

Could be just personal taste (no pun intended) but I found this analogy more confusing than helpful and has got me wanting to eat pudding when I don't have any in my fridge :( I think Mark Ransom's answer, although more boring, is probably more instructive

@Amadan 2015-10-01 06:49:44

@TomClose: There are many fine answers on this question, enough so that there is space for levity. Also, I want pudding too.

@Mark Ransom 2012-11-30 17:44:11

is only returns true if they're actually the same object. If they were the same, a change to one would also show up in the other. Here's an example of the difference.

>>> x = [1, 2, 3]
>>> y = [1, 2, 3]
>>> print x is y
False
>>> z = y
>>> print y is z
True
>>> print x is z
False
>>> y[0] = 5
>>> print z
[5, 2, 3]

@CS Gamer 2012-11-30 17:42:40

As you can check here to a small integers. Numbers above 257 are not an small ints, so it is calculated as a different object.

It is better to use == instead in this case.

Further information is here: http://docs.python.org/2/c-api/int.html

@mipadi 2012-11-30 17:41:35

It compares object identity, that is, whether the variables refer to the same object in memory. It's like the == in Java or C (when comparing pointers).

@Neko 2012-11-30 17:40:31

X points to an array, Y points to a different array. Those arrays are identical, but the is operator will look at those pointers, which are not identical.

@David Heffernan 2012-11-30 17:40:50

Python doesn't have pointers. You need to tighten up your terminology.

@Neko 2012-11-30 17:41:47

It does internally, just like Java and so many other languages. In fact, the is operator's functionality shows this.

@David Heffernan 2012-11-30 17:45:22

The implementation details are not what matters. The documentation uses the terminology "object identity". So should you. "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."

@abarnert 2014-09-10 05:40:33

@Neko: CPython internally uses pointers. But obviously Jython (implemented in Java) and PyPy (implemented in a subset of Python) don't use pointers. In PyPy, some objects won't even have an id unless you ask for it.

Related Questions

Sponsored Content

26 Answered Questions

34 Answered Questions

[SOLVED] What is the !! (not not) operator in JavaScript?

  • 2009-04-24 08:13:58
  • Hexagon Theory
  • 438555 View
  • 2603 Score
  • 34 Answer
  • Tags:   javascript operators

18 Answered Questions

[SOLVED] Reference — What does this symbol mean in PHP?

31 Answered Questions

[SOLVED] Understanding slice notation

  • 2009-02-03 22:31:02
  • Simon
  • 1350060 View
  • 2584 Score
  • 31 Answer
  • Tags:   python list slice

16 Answered Questions

[SOLVED] Convert bytes to a string?

21 Answered Questions

[SOLVED] What is the "-->" operator in C++?

10 Answered Questions

32 Answered Questions

[SOLVED] "Least Astonishment" and the Mutable Default Argument

7 Answered Questions

Sponsored Content