473,394 Members | 1,875 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,394 software developers and data experts.

Seemingly odd 'is' comparison.

>>print float(3.0) is float(3.0)
True
>>print float(3.0 * 1.0) is float(3.0)
False
>>>

Thanks,

Tobiah

--
Posted via a free Usenet account from http://www.teranews.com

Feb 18 '08 #1
4 1101
On Feb 13, 10:19*pm, Tobiah <t...@tobiah.orgwrote:
>print float(3.0) is float(3.0)
True
>print float(3.0 * 1.0) is float(3.0)
False
[You don't need to wrap your floats in float()]
>>def f():
... return 3.0 is 3.0, 3.0*1.0 is 3.0
...
>>f()
(True, False)
>>import dis
dis.dis(f)
2 0 LOAD_CONST 1 (3.0)
3 LOAD_CONST 1 (3.0)
6 COMPARE_OP 8 (is)
9 LOAD_CONST 3 (3.0)
12 LOAD_CONST 1 (3.0)
15 COMPARE_OP 8 (is)
18 BUILD_TUPLE 2
21 RETURN_VALUE

As you can see when "3.0 is 3.0" is evaluated the same float object is
put on the stack twice so the 'is' comparison is True (LOAD_CONST 1 /
LOAD_CONST 1 / COMPARE_OP 8).

Whereas when "3.0*1.0 is 3.0" is evaluated, *two* different float
objects are put on the stack and compared (LOAD_CONST 3 / LOAD_CONST
1 / COMPARE_OP 8). Therefore the result is False.

HTH

--
Arnaud

Feb 18 '08 #2
Tobiah wrote:
>>>print float(3.0) is float(3.0)
True
>>>print float(3.0 * 1.0) is float(3.0)
False
>>>>
Thumb rule: Never compare strings, numbers or tuples with "is". Only
compare an object with a singleton like a type or None. "is" is not a
comparison operator.

Christian

Feb 18 '08 #3
Tobiah wrote:
>>>print float(3.0) is float(3.0)
True
>>>print float(3.0 * 1.0) is float(3.0)
False
>>>>
It's implementation dependent what values these expressions will take.

If you're trying to test equality, use `==`, not `is`.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
There is nothing more incomprehensible than a wrangle among
astronomers. -- H.L. Mencken
Feb 18 '08 #4
On Feb 19, 12:27 pm, Asun Friere <afri...@yahoo.co.ukwrote:
But given the nature of immutables
I meant to write "given the nature of mutables" of course ... :/
Feb 19 '08 #5

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

Similar topics

1
by: Bill W. | last post by:
I'm teaching myself VB6. As a first project, I've decided to work on a Window's version of the Unix command, "diff" -- with a few twists. But being unfamiliar with VB, I am unsure as to how to...
1
by: David Pratt | last post by:
I have been using the following for sorting a list of dictionaries. This works but only provides sorting on a single key. I am wanting to extend this with a better comparison expression so that it...
6
by: sam | last post by:
Hi, I got the the following syntax error in comparison: File "/usr/local/work/myparser.py", line 85 if ( (m=self.macro_parser.match (d)) != None ): ^ SyntaxError: invalid syntax How can I...
3
by: aaj | last post by:
Hi all I have Aceess 2000 front end linking to SQLServer 2000 BE. While developing the backend I work on an offline database. When happy I update the live one. Any changes I've made to the...
6
by: aurgathor | last post by:
Howdy, How do I pass some function a generic comparison function? I figured out one non-generic case, but since this code got parameter declarations in two places, it's obviously not generic....
6
by: Doug | last post by:
Hello, Can anyone recommend an inexpensive source code comparison utility for .NET. I broke my code an now I need to compare my older version w/ the new one and could use the help of line...
18
by: PicO | last post by:
how can i make a set with comparison function ? all i know that i can make a map with comparison function like this struct strCmp { bool operator()( const char* s1, const char* s2 ) const {...
12
by: Zilla | last post by:
I put everything in one file for ease of use; also I have #include <*.hfiles instead of the <*since my compiler is pre-ANSI C so it needs the .h #include <iostream.h> #include <iomanip.h>...
4
by: Stef Mientki | last post by:
hello, I had a program that worked perfectly well. In this program modules were dynamically added, just by putting the file in a predefined directory. Now one of the interface mechanisms was...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.