About the implementation of del in Python 3

Steve D'Aprano steve+python at pearwood.info
Fri Jul 7 04:24:45 EDT 2017


On Fri, 7 Jul 2017 05:29 pm, Dan Wissme wrote:

> Strange behavior in Python 3.6.0
> >>> i = 3000
> >>> j = 3000
> >>> i is j
> False
> >>> n = 4000 ; m = 4000 ; n is m
> True


The Python interpreter is allowed to cache integers and reuse them. The
interactive interpreter sometimes does so: if you write the same int literal on
the same line in the interactive interpreter, it may re-use the same object
instead of creating two equal objects.

You should **NEVER** use `is` when you want to check for equality. You cannot
rely on Python to cache or not cache int values. Whatever it does is purely an
implementation detail that is subject to change without notice.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list