'==' vs. 'is' behavior

Neil Schemenauer nascheme at enme.ucalgary.ca
Tue Nov 30 00:13:40 EST 1999


François Pinard <pinard at iro.umontreal.ca> wrote:
>
>By the way, is there common wisdom (or rather, maybe, usage-standards-to-be)
>about using `is' instead of `=='?

`is' will always return 0 if == returns 0 (ie. it is a finer
distinction).  `is' is slightly faster in some cases.  The only
places I use `is' is when testing if instances of objects are the
same and when testing for None.  To me, `is' means that something
is the same object.

Using `is' interchangably with == is asking for trouble.  It
works most of the time (just enough to screw you) due to
implementation details (interning of strings, caching of small
integers, etc.). 

    >>> a = 'a'
    >>> b = 'a'
    >>> a is b
    1
    >>> b = raw_input()
    b
    >>> b
    'b'
    >>> a is b
    0

Your lucky you don't do Lisp.  In scheme (r5rs) there is:

    eqv?
    eq?
    equal?

and in Common Lisp:

    eq
    eql
    equal
    equalp
    =
    char=

Python is absolute simplicity is comparison.


    Neil

-- 
python -c "f=lambda n:n and f(n/128)+chr(n%128) or '';print f(0x13b2\
e9d8829e3d1976e5dd87ae5e481e6ec3cf1e8cbb72c0eb8f0eccf879795d8f0bel)"




More information about the Python-list mailing list