473,799 Members | 3,006 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 #1
15 11210
daniel wrote:
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
'is' compares object identity. == compares values.
>>a = [1, 2, 3]
b = [1, 2, 3]
a is b
False
>>a == b
True

In this example, a and b refer to two separate lists that happen to hold
the same values. Thus, 'is' returns False, and == returns True. On the
other hand:
>>c = d = [4, 5, 6]
c is d
True
>>c == d
True

Here, c and d refer to the same list. Therefore, 'is' returns true (they
both refer to the same object), as does == (an object is always equal to
itself, unless you overload the equality check in a weird way).

The distinction can easily be seen if we try to mutate these lists:
>>a.append(4)
a is b
False
>>a == b
False
>>c.append(7)
c is d
True
>>c == d
True

When we mutate a, b is not affected. They are two different lists, and
changing 'a' makes it so they are no longer equal.

When we mutate c, d IS affected; they refer to the same list.

You can easily confuse yourself if you ever talk about applying 'is' to
(for example) integers. Python may re-use certain small integers when
you might not expect it to; this is done in the interests of efficiency.
If you only compare the /values/ of numbers (with ==), then you will
never notice this.
>>a = 1
b = 1
c = 1000000
d = 1000000
a is b
True
>>c is d
False

-Kirk McDonald
Aug 15 '06 #2
many thanks to Sybren and Kirk for your helpful explanation.

when I tried to check the stuff out, found sth interesting that if you
define variables in a style like this:
a = b = ['a', 'b']
changing one list affects the other, and they still refer to same
object. in fact, seems all compound types (dictionary for instance)
behave in this way.

however, when list is replaced with other built-in types like integers
:
a = b = 3
changing one of them cause the two objects differ...
best regards.

daniel

Aug 15 '06 #3
daniel wrote:
when I tried to check the stuff out, found sth interesting that if you
define variables in a style like this:
a = b = ['a', 'b']
changing one list affects the other, and they still refer to same
object. in fact, seems all compound types (dictionary for instance)
behave in this way.

however, when list is replaced with other built-in types like integers
:
a = b = 3
changing one of them cause the two objects differ...
Ah, but make a difference between "change a variable", and "change an
object".

pya = b = [1,2,3]
pya[0] = 6 # don't change the variable a, just change the object
pya
[6, 2, 3]
pyb
[6, 2, 3]
pya=[7,8,9] # change the variable a;
# it's now a different object than b
pya
[7, 8, 9]
pyb
[6, 2, 3]

For some objects, "change the object" is impossible. If you have

a = b = 3

then there is no way to change the object 3 to become 4 (say);
integer objects are "immutable" . So for these, to make a change,
you really have to change the variable, not the value.

Regards,
Martin
Aug 15 '06 #4

Martin v. Löwis wrote:
daniel wrote:
when I tried to check the stuff out, found sth interesting that if you
define variables in a style like this:
a = b = ['a', 'b']
changing one list affects the other, and they still refer to same
object. in fact, seems all compound types (dictionary for instance)
behave in this way.

however, when list is replaced with other built-in types like integers
:
a = b = 3
changing one of them cause the two objects differ...

Ah, but make a difference between "change a variable", and "change an
object".

pya = b = [1,2,3]
pya[0] = 6 # don't change the variable a, just change the object
pya
[6, 2, 3]
pyb
[6, 2, 3]
pya=[7,8,9] # change the variable a;
# it's now a different object than b
mm, python runtime might allocate a new chunk of memory for this... but
might not for the previous operation..
pya
[7, 8, 9]
pyb
[6, 2, 3]

For some objects, "change the object" is impossible. If you have

a = b = 3

then there is no way to change the object 3 to become 4 (say);
integer objects are "immutable" . So for these, to make a change,
you really have to change the variable, not the value.
sounds reasonable, I tried tuple which is also immutable, it behaves
the same as integers.
Regards,
Martin
tks Martin...

Aug 15 '06 #5
daniel wrote:
Martin v. Löwis wrote:
[...]
>>For some objects, "change the object" is impossible. If you have

a = b = 3

then there is no way to change the object 3 to become 4 (say);
integer objects are "immutable" . So for these, to make a change,
you really have to change the variable, not the value.

sounds reasonable, I tried tuple which is also immutable, it behaves
the same as integers.
Well spotted. Tuples are indeed immutable, as are strings, unicode
strings, integers and floats.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Aug 15 '06 #6

"Sybren Stuvel" <sy*******@YOUR thirdtower.com. imaginationwrot e in message
news:sl******** **************@ schuimige.stuve l.eu...
'is' compares the object's addresses.
It actually compares the objects' integer identifiers. That happens to be
the linear memory address for CPython, but not necesarily so for other
interpreters.

tjr

Aug 15 '06 #7
Sybren Stuvel wrote [on the difference between is and ==]:
Obviously "a is b" implies "a == b",
Not necessarily.
>>a = b = 1e1000 / 1e1000
a is b
True
>>a == b
False

Aug 15 '06 #8

Steve Holden wrote:
daniel wrote:
Martin v. Löwis wrote:
[...]
>For some objects, "change the object" is impossible. If you have

a = b = 3

then there is no way to change the object 3 to become 4 (say);
integer objects are "immutable" . So for these, to make a change,
you really have to change the variable, not the value.
sounds reasonable, I tried tuple which is also immutable, it behaves
the same as integers.
Well spotted. Tuples are indeed immutable, as are strings, unicode
strings, integers and floats.
But tuples can contain mutable objects.
>>a = (0, [1])
a[1].append(2)
a
(0, [1, 2])

Aug 15 '06 #9

"Dan Bishop" <da*****@yahoo. comwrote:
| Sybren Stuvel wrote [on the difference between is and ==]:
| Obviously "a is b" implies "a == b",
|
| Not necessarily.
|
| >>a = b = 1e1000 / 1e1000
| >>a is b
| True
| >>a == b
| False

Huh? - wtf is this - I find this deeply disturbing - Sybren's explanation kind
of was congruent with my own understanding, and this is just weird - Hendrik

Aug 16 '06 #10

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

Similar topics

15
2558
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
1990
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
2316
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
3669
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
9546
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10490
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...
0
10260
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10030
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9078
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
7570
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
6809
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
5467
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...
3
2941
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.