Legitimate use of the "is" comparison operator?

Mike Duffy Mike.S.Duffy at gmail.com
Sat Jun 17 04:34:13 EDT 2006


Fredrik Lundh wrote:
>
> except that it doesn't work.
>
> writing broken code is never a good practice.
>
With all due respect, for some reason it seems to work on my machine.
Because I certainly agree with you about writing broken code.

Python 2.4.2 (#1, Jan 17 2006, 16:52:02)
[GCC 4.0.0 20041026 (Apple Computer, Inc. build 4061)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = range(5)
>>> b = range(5)
>>>
>>> if len(a) is len(b):
...     print "They're the same size!"
... else:
...     print "They're not the same size!"
...
They're the same size!
>>>

>
> (the reason that it appears to work for small integers is that the
> interpreter is caching the objects for some commonly used values,
> including small integers and one-character strings.  but that's an
> interpreter implementation detail, not something you can rely on).
> 
That's very interesting. Thank you for explaining  :-)




More information about the Python-list mailing list