'is' versus '=='

Aaron Ginn aaron.ginn at motorola.com
Mon May 7 13:04:34 EDT 2001


Maybe I have an incorrect understanding of the 'is' operator, but I
was under the impression that 'is' and '==' operated the same way.

Could someone explain to me why I am seeing different functionality
in the following example.

-----------------------------------------------------
ginn at coronado download $ python
Python 2.0 (#1, Feb 20 2001, 09:09:57)
[GCC 2.95.2 19991024 (release)] on sunos5
Type "copyright", "credits" or "license" for more information.
>>> a='-o'
>>> if a is '-o':
...     print 'Good'
... else:
...     print 'Bad'
...
Bad
>>> a='-o'
>>> if a == '-o':
...     print 'Good'
... else:
...     print 'Bad'
...
Good
-----------------------------------------------------

The first case simply doesn't make sense to me.  a is clearly equal to 
'-o', but the 'is' operator is not giving me an expected result (at
least what _I_ expect).  The second case using the '==' is behaving
the way I would expect it to.  I suspect it is related to the '-' in
the string because in the following example, 'is' behaves correctly:

-----------------------------------------------------
ginn at coronado download $ python
Python 2.0 (#1, Feb 20 2001, 09:09:57)
[GCC 2.95.2 19991024 (release)] on sunos5
Type "copyright", "credits" or "license" for more information.
>>> a='A'
>>> if a is 'A':
...     print 'a is ' + a
...     print 'OK'
... else:
...     print 'a is ' + a
...     print 'Uh oh!'
...
a is A
OK
>>> a='-A'
>>> if a is '-A':
...     print 'a is ' + a
...     print 'OK'
... else:
...     print 'a is ' + a
...     print 'Uh oh!'
...
a is -A
Uh oh!
-----------------------------------------------------


What am I missing here?

Thanks,
Aaron Ginn

-- 
Aaron J. Ginn                    Phone: 480-814-4463
Motorola SemiCustom Solutions    Pager: 877-586-2318
1300 N. Alma School Rd.          Fax  : 480-814-4058
Chandler, AZ 85224 M/D CH260     mailto:aaron.ginn at motorola.com



More information about the Python-list mailing list