is this a bug?

Skip Montanaro skip at pobox.com
Wed Aug 15 19:47:29 EDT 2001


    Bill> Do you find anything irrational to the following session?

    >>> a='+='
    >>> a is a
    1
    >>> a is '+='
    0

Nope.  "is" simply compares the two PyObject * pointers represented by the
variable "a" and the string literal "+=".  The second "+=" is a different
string object than the first one.  There is no obligation on the part of the
interpreter to intern any or all strings it encounters, and it doesn't in
the typical case.

Try this:

    >>> def f():
    ...   a = "+="
    ...   print a is a
    ...   print a is "+="
    ... 
    >>> f()
    1
    1

It is left as an exercise for the reader why the result of executing f()
produces different results than the previous interactive session.  ;-)

-- 
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/




More information about the Python-list mailing list