0

I've been banging my head with the following code...

code = (404, 200)

if code[0] is 404:
    print "yes"

if code[1] is 200:
    print "yes"

should return true both right? but just the condition of the number 200 and true why?

  • Use `==` for comparison, not `is` – TerryA Jan 05 '18 at 01:14
  • `is` tells you if they're both the same object. Python can decide to have duplicate objects or not depending on its whims, if the objects are immutable. – Mark Ransom Jan 05 '18 at 01:14
  • @TerryA He easily could, but the point is to understand why this is happening, not just avoid the problem. – nog642 Jan 05 '18 at 01:15
  • 1
    @William PY -5 - 256 are cached in python. 404 isn't, so they have different id's which `is` is testing for object identity. Try printing out id(code[0]), id(code[1]) and id(200), id(404) – Pythonista Jan 05 '18 at 01:17
  • @nog642 Yes, hence the closure – TerryA Jan 05 '18 at 01:18

0 Answers0