is and ==

Dave Brueck dbrueck at edgix.com
Mon Feb 26 12:00:35 EST 2001


> [mailto:python-list-admin at python.org]On Behalf Of Steve Purcell
> Sent: Monday, February 26, 2001 9:50 AM
> To: Daniel Klein
> Cc: python-list at python.org
> Subject: Re: is and ==

< snip >
> Integers are both equal *and* identical:
>
> >>> a = 1
> >>> b = 1
> >>> a is b
> 1
> >>> a == b
> 1

Not necessarily:

>>> a = 10000
>>> b = 10000
>>> a is b
0
>>> a = 5
>>> b = 5
>>> a is b
1

The interpreter "caches" integer objects from something like -1 to 100, so
'is' evaluates to true for low integers but not higher integers.

(Also, as the docs point out, this caching means that from inside an
extension module you could _redefine_ the value of the integer object 5, for
example:

>>> evilext.causehavoc(5,10) # sets '5' object to have int value of 10
>>> 5 + 5
20  # Whee!!!

)

-Dave





More information about the Python-list mailing list