473,811 Members | 3,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is the keyword "is" for?

I'm so confused by the keyword "is" and "==" equal sign, it seems they
could be exchanged in some contexts, but not in others, what's the
difference between them in terms of comparation?

thanks...

daniel

Aug 15 '06
15 11212
Sybren Stuvel wrote:
Dan Bishop enlightened us with:
>>a = b = 1e1000 / 1e1000
a is b
True
>>a == b
False

If "a is b" then they refer to the same object, hence a == b. It
cannot be otherwise, unless Python starts to defy logic. I copied your
code and got the expected result:
>a = b = 1e1000 / 1e1000
a is b
True
>a == b
True
Probably depends on the implementation. 1e1000/1e1000 yields a NaN
here, and I get True for "a is b" but False for "a==b". Presumably
comparing a NaN for equality (any comparison?) always yields False.

Aug 16 '06 #11
Sybren Stuvel wrote:
Dan Bishop enlightened us with:
>>a = b = 1e1000 / 1e1000
a is b
True
>>a == b
False

If "a is b" then they refer to the same object, hence a == b. It
cannot be otherwise, unless Python starts to defy logic. I copied your
code and got the expected result:
>a = b = 1e1000 / 1e1000
a is b
True
>a == b
True

Sybren
Python 2.4.3 (#2, Apr 27 2006, 14:43:58)
[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.

|>a = b = 1e1000 / 1e1000
|>a is b
True
|>a == b
False

Huh. Weird. I copied it too and got the "wrong" result.

Aug 16 '06 #12
In <sl************ **********@schu imige.stuvel.eu >, Sybren Stuvel wrote:
Dan Bishop enlightened us with:
>>>>a = b = 1e1000 / 1e1000
a is b
True
>>>>a == b
False

If "a is b" then they refer to the same object, hence a == b. It
cannot be otherwise, unless Python starts to defy logic. I copied your
code and got the expected result:
>>>a = b = 1e1000 / 1e1000
a is b
True
>>>a == b
True
I get the same as Dan:

In [13]: a = b = 1e1000 / 1e1000

In [14]: a is b
Out[14]: True

In [15]: a == b
Out[15]: False

In [16]: a
Out[16]: nan

On my platform the division results in "Not A Number". Two NaNs compared
are always `False`. You could argue that this is the very same NaN but to
get this effect the interpreter has to take care that every NaN produced
while a program is running is unique. Quite huge overhead for such a
corner case IMHO.

Ciao,
Marc 'BlackJack' Rintsch
Aug 16 '06 #13
On Wed, 16 Aug 2006 10:06:03 +0200,
Marc 'BlackJack' Rintsch <bj****@gmx.net wrote:
In [14]: a is b
Out[14]: True
In [15]: a == b
Out[15]: False
In [16]: a
Out[16]: nan
On my platform the division results in "Not A Number". Two NaNs
compared are always `False`. You could argue that this is the very
same NaN but to get this effect the interpreter has to take care that
every NaN produced while a program is running is unique. Quite huge
overhead for such a corner case IMHO.
The interpreter isn't doing anything special; nans have [the equivalent
of] an __eq__ method that always returns False.

Regards,
Dan

--
Dan Sommers
<http://www.tombstoneze ro.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist
Aug 16 '06 #14
Simon Forman <ro*********@ya hoo.comwrote:
>Python 2.4.3 (#2, Apr 27 2006, 14:43:58)
[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.

|>a = b = 1e1000 / 1e1000
|>a is b
True
|>a == b
False
I agree with you:

$ python
Python 2.4.1 (#2, May 5 2005, 11:32:06)
[GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
>>a = b = 1e1000 / 1e1000
a is b
True
>>a == b
False

Or maybe I don't:

$ python2.3
Python 2.3.5 (#2, Sep 4 2005, 22:01:42)
[GCC 3.3.5 (Debian 1:3.3.5-13)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
>>a = b = 1e1000 / 1e1000
a is b
True
>>a == b
True

See:

http://mail.python.org/pipermail/pyt...ry/022133.html

--
\S -- si***@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Aug 16 '06 #15
Sybren Stuvel <sy*******@YOUR thirdtower.com. imaginationwrot e:
Dan Bishop enlightened us with:
>>a = b = 1e1000 / 1e1000
a is b
True
>>a == b
False

If "a is b" then they refer to the same object, hence a == b. It
cannot be otherwise, unless Python starts to defy logic. I copied your
Python also needs to respect standards (such as IEEE 754 for
floating-point arithmetic, and the SQL standards) which do specify the
existence of special objects that "are not equal to anything" including
themselves -- Nan and NULL respectively for these two standards.

We're talking about extremely widespread international standards
developed by huge body of professionals which do include professional
logicians, so I doubt they "defy logic". Python tries to delegate FP to
the underlying hardware and SQL to an external relational DB engine, so
it should be as compliant as the pieces of infrastructure it's using --
which seems to me to be a good architectural decision (not just for
speed and ease of coding, but to ensure any anomaly is somebody else's
fault:-).
Alex
Aug 16 '06 #16

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

Similar topics

15
2562
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 little clearer to me. I now have some projects coming up where the baseTypeCollection will contain 5k to 10k objects that will have to be searched for various sub types of which I only expect to find ~50 to ~100 matches. Thanks! foreach (baseType...
6
1992
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 this: - a DLL which contains the interface that every plugin must implement (IPlugin). This DLL also contains a class with the ability to search for DLLs in the calling applications working directory that contain classes implementing IPlugin (I...
17
2319
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 T, typename U > Boolean isinst(U u) {
6
1406
by: newcomer2k | last post by:
Hi, I understand how to use the "is" keyword straight forward. like: if (obj is MyClass1) However, I would like to parametize the "is"'s argument. Basically, I would to replace MyClass1 with an argument. Here is an example: private void foo(WhatType arg) { foreach (Control uc in _displayPanel.Controls)
8
12940
by: Zytan | last post by:
In VB, you use ControlChars.CrLf (or better, ControlChars.NewLine). In C/C++, we are used to using merely "\n" (or at least I am). It appears "\n" is good enough for RichTextBoxes. Does it matter? I am so used to typing "\n" that I don't think this habit will change anyday soon. Zytan
81
3677
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 understand why this works: if text is None: and why this always returns false:
0
10647
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10398
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.