Legitimate use of the "is" comparison operator?

M

Mike Duffy

I just recently realized that the comparison operator "is" actually
works for comparing numeric values. Now, I know that its intended use
is for testing object identity, but I have used it for a few other
things, such as type checking, and I was just wondering whether or not
it is considered bad practice in the Python Community to use it for
numerics as well.

Example:

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!"
 
F

Fredrik Lundh

Mike said:
I just recently realized that the comparison operator "is" actually
works for comparing numeric values.

except that it doesn't work.
Now, I know that its intended use is for testing object identity, but
I have used it for a few other things, such as type checking, and I
was just wondering whether or not it is considered bad practice in
the Python Community to use it for numerics as well.

writing broken code is never a good practice.
.... print "same size"
.... else:
.... print "not the same size"
....
not 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).

</F>
 
M

Mike Duffy

Fredrik said:
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..... 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 :)
 
F

Fredrik Lundh

Mike said:
With all due respect, for some reason it seems to work on my machine.

if you always work with 5-item sequences, you don't need the test at all.

</F>
 
B

bruno at modulix

Mike said:
I just recently realized that the comparison operator "is" actually
works for comparing numeric values.

It's only an implementation detail of CPython (and is only true for
small integers - you'll find the limit in the CPython source code), not
part of the language specifications. You should *not* relie on this
behaviour.
Now, I know that its intended use
is for testing object identity, but I have used it for a few other
things, such as type checking,

Don't use it for this neither unless you know exactly what you do. Use
isinstance(obj, klass) instead - and yet better, don't check type at all
if you can avoid it.
and I was just wondering whether or not
it is considered bad practice in the Python Community to use it for
numerics as well.

It's even worse than a bad practice : it's an error.

Example:

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!"
False
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top