1

Possible Duplicate:
Python β€œis” operator behaves unexpectedly with integers
Why (0-6) is -6 = False?

>>> (0 - 10) is -10
False
>>> (0 - 5) is -5
True

Can anybody explain me , how I am getting False in one case and True in another case for same manipulation...

Some more weirdness...

>>> (0 - 10) is (0 - 10)
False
Community
  • 1
  • 1
NIlesh Sharma
  • 5,445
  • 6
  • 36
  • 53

5 Answers5

3

Don't use is for equality testing - it's the identity operator, and two equal objects are not necessarily identical (i. e. residing under the same address in your computer's memory). Some small integers may be cached and reused but not all of them which is why you're seeing different results here.

Additionally, this behavior is implementation-dependent, so you can't rely on this always being the case.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
2

From the doc:

The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object. So it should be possible to change the value of 1. I suspect the behaviour of Python in this case is undefined. :-)

And from my experience: In my Python2.7, (0 + x) is x returns True for x between -5 and +256, False elsewhere.

eumiro
  • 207,213
  • 34
  • 299
  • 261
1

You are creating instances of int and expecting them to all be the same object. This doesn't normally work at all for most other objects, so perhaps it should be surprising that you get True at all.

The reason for this is that Python creates a bunch of frequently used integer objects at startup, that can be reused whenever they are called for. This works out ok because int are immutable, but is simply an optimisation.

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
1

The is operator will determine whether two objects have the same identity (in low-level terms, whether their representations are at the same address in memory).

a = object()
a is a
True

In Python, numbers are objects just like anything else, so whether is works will depend on how you created them.

a = 0
a is a
True

The reason that your code works in one case and not in the other is that Python interns small integers; that is, rather than creating a new number object for -5 it will keep a cache of small integers and give you the appropriate number object. On the other hand in your case -10 is not interned so Python has to create a new number object each time.

Other objects that are interned include short strings (including single-character strings), and True and `False.

You should not be relying on interning; instead of is you should use the == operator.

ecatmur
  • 152,476
  • 27
  • 293
  • 366
0

There's probably some implementation optimization that may reuse the same number object.

I'd say that's why you're getting True sometimes.

$ python3
Python 3.2.3 (default, May  3 2012, 15:51:42)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> id(3)
9357472
>>> id(3)
9357472

What I think you're looking for an equlity check with ==:

>>> 3 is 3    # Result may depend on implementation details
True
>>> 3 == 3    # Always True
True
Rik Poggi
  • 28,332
  • 6
  • 65
  • 82