is this a bug?

Brian Quinlan BrianQ at ActiveState.com
Wed Aug 15 19:41:02 EDT 2001


> I apologize if this has been brought to the attention of this
> group before. Maybe I did misunderstand the use of the 'is'
> operator, but then how it is supposed to work?

There is a difference between the "is" and "==" operators:
"==" assess equivalence
"is" assess identity

So "is" will return 1 iff the underlying objects being compared are
the same objects. There is a bit of a gotcha here because Python will
try to reuse objects when it can do so e.g.

>>> a = 1
>>> b = 1
>>> a is b # small numbers are precreated for reuse
1
>>> a = 1000
>>> b = 1000
>>> a is b # large ones are not
0

The bottom line is that you should rarely use the "is" operator except
when testing if an object is None (there is only one None object in
Python).

Cheers,
Brian





More information about the Python-list mailing list