1

Possible Duplicates:
Is there any difference between “foo is None” and “foo == None”?
Python '==' vs 'is' comparing strings, 'is' fails sometimes, why?

Hi, im doing some checks on argvs in python and running from the windows cmd

python script.py 2.zap

This works(rises the desired Exception):

from sys import argv    
if argv[1][-3:] != "zip":
        raise Exception()

But this doesnt

from sys import argv    
    if argv[1][-3:] is not "zip":
        raise Exception()

Could you explain the difference, thanks in advance. I'm using python 3 if thats relevant.

Community
  • 1
  • 1
Guillermo Siliceo Trueba
  • 4,251
  • 5
  • 34
  • 47
  • 1
    http://stackoverflow.com/questions/2419361/python-difference-between-is-and – ClosureCowboy Mar 05 '11 at 17:27
  • Just replace `__eq__` in related question with `__ne__` and see the Operator section in http://docs.python.org/reference/datamodel.html –  Mar 05 '11 at 17:32
  • Ok thanks i read those, as i understood "is not" test if the object is not the same. My conclusion is that argv is a python list(an object) and if do some operation on that object, like getting one of its members argv[1] and then slicing that member argv[1][-:3] i'm not actually creating a new object, is just a temporary thing that "is not" cannot check against, because "is not" needs real objects to work with. Is that correct? – Guillermo Siliceo Trueba Mar 05 '11 at 17:33
  • 1
    No. No new objects are exposed in this example. `is` and `is not` are *identity-equals* (or not-identity-equals) while `==` and `!=` are *equality-equals* (or not-equality-equals). Compare: `"x" is "x"` (false) vs. `x = "x"; x is x` (true) vs. `"x" == "x"` (true). The questions this has been closed for should help to clarify. An object is only identity-equal with itself. (Integer numbers are slightly special in this regard). –  Mar 05 '11 at 17:37

0 Answers0