473,385 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Legitimate use of the "is" comparison operator?

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

Jun 17 '06 #1
4 1312
Mike Duffy wrote:
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.
a = range(10000)
if len(a) is len(a):

.... 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>

Jun 17 '06 #2
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 :-)

Jun 17 '06 #3
Mike Duffy wrote:
writing broken code is never a good practice.

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>

Jun 17 '06 #4
Mike Duffy wrote:
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!"
a = range(32000)
b = range(32000)
len(a) is len(b)

False
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jun 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: hjbortol | last post by:
Hi! Is the expression "a >= b" equivalent to "a - b >= 0" in C/C++? Is this equivalence an IEEE/ANSI rule? Or is this machine/compiler dependent? Any references are welcome! Thanks in...
15
by: Bobby C. Jones | last post by:
Is there an advantage to either of these methods, or is it simply a matter of style? I used to use the "as" method, but later gave it up for the "is" method as the intent of the code seems a...
6
by: Derrick | last post by:
Hello all; Since I do have working code, this is more for my curiosity only. I'm creating a "Plugin" architecture, following some of the many examples on the 'net. Basically what I have is...
3
by: JimM | last post by:
I am trying to create a method in VS 2003 that validates an object argument is of the proper type and within a range of values. I am trying to use a Type to define the casting and object type for...
9
by: Lonnie Princehouse | last post by:
There doesn't seem to be any way to customize the behavior of "is" as can be done for other operators... why not?
4
by: Johndow | last post by:
I have trouble to use "Is" operator to a type that is from reflection. Basically, here is what I want to do, if Class1 type is not from reflection: class Class1 { } public class Test { ... if...
17
by: nicolas.hilaire | last post by:
Hi all, i've read this article http://msdn2.microsoft.com/en-us/library/85af44e9.aspx who first interest me much. I've translated it to use generic instead of template : generic < typename ...
7
by: Kenneth Brody | last post by:
(From something brought up on "Help with array/pointer segmentation fault needed" thread.) Is "?" a sequence point? Or, more directly, is the following defined? /* Will "ptr" be guaranteed...
81
by: BlueJ774 | last post by:
Can someone please explain to me the difference between the "is" keyword and the == boolean operator. I can't figure it out on my own and I can't find any documentation on it. I can't...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.