473,397 Members | 2,084 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,397 software developers and data experts.

Surprise using the 'is' operator

I thought the 'is' operator was used to identify identical objects,
whereas the '==' operator checked equality. Well, I got a surprise
here:

IDLE 1.1.3
>>a = 10
b = a
a is b
True
>>a == b
True
>>c = 10
a == c
True
>>a is c
True
>>>
I was NOT expecting the last statement to return True!

What am I missing?

Thanks
Tony

Sep 26 '06 #1
10 1114
codefire wrote:
I thought the 'is' operator was used to identify identical objects,
whereas the '==' operator checked equality. Well, I got a surprise
here:

IDLE 1.1.3
>>>a = 10
b = a
a is b
True
>>>a == b
True
>>>c = 10
a == c
True
>>>a is c
True
>>>>

I was NOT expecting the last statement to return True!

What am I missing?
One of the most severely beaten dead horses of python - small integers are
cached by the interpreter. Maybe. Thus the above results.

Google for a bazillion discussions on this.

Diez
Sep 26 '06 #2
codefire a écrit :
I thought the 'is' operator was used to identify identical objects,
whereas the '==' operator checked equality. Well, I got a surprise
here:

IDLE 1.1.3
>>>a = 10
b = a
a is b
True
>>>a == b
True
>>>c = 10
a == c
True
>>>a is c
True

I was NOT expecting the last statement to return True!
The answer is : Why not? Does it even matter for integers? Never use
"is" on integers, always == ( and on strings too for that matter )
Sep 26 '06 #3
Haha!

OK thanks guys.

I was just trying to check if objects were the same (object), didn't
know Integers were a special case.

Thanks,
Tony

Sep 26 '06 #4
codefire wrote:
I was just trying to check if objects were the same (object), didn't
know Integers were a special case.
they're not, really; "is" works the same way for all objects.

when you ask for a new immutable object, a Python implementation may
always reuse an existing object, if it wants to.

the CPython implementation does this for small integers, booleans, None,
empty tuples, certain strings, type objects, etc. this also applies to
library code; for example, string methods often return a reference to
"self" if the method didn't actually change anything.

</F>

Sep 26 '06 #5
Thanks for that Fredrik, that's clear. That's actually a pretty nice
feature as it's nicely optimised.
>>a = 10
c = 10
a is c
True
>>c = c +1
a is c
False
>>>
Cheers,
Tony

Sep 26 '06 #6
codefire a écrit :
Haha!

OK thanks guys.

I was just trying to check if objects were the same (object), didn't
know Integers were a special case.
They are not a special case so much. It's just that "is" is extremly
unreliable unless you're the one who created the objects. In the case of
integers, it's the CPython implementation which constructs them as it
sees fit and so, you cannot expect any kind of reliable "is" behavior.

When you are manipulating let's say lists, it gets much more reliable.
You do know that the list you create by doing a = [] is not and will
never be the same than the one you'll create later by doing b = []
Sep 26 '06 #7
"codefire" <to**********@gmail.comwrites:
I was just trying to check if objects were the same (object), didn't
know Integers were a special case.
They're not. The Python runtime environment can do whatever it likes
underneath the hood; the language gives no promises about any
relationship between 'is' and '=='.

What you may be missing is that 'c = 10' is not "putting the value 10
into the box named c". It is rather "putting the label c onto the
object 10".

Whether there are one, two or twelve instances of an object '10' is
entirely up to the Python runtime implementation. You can use 'is' to
check whether two objects are identical; you can use '==' to check
whether they are equal in value; you cannot generalise anything about
the relationship between those two.

--
\ "I still have my Christmas Tree. I looked at it today. Sure |
`\ enough, I couldn't see any forests." -- Steven Wright |
_o__) |
Ben Finney

Sep 26 '06 #8
On 2006-09-26, Fredrik Lundh <fr*****@pythonware.comwrote:
codefire wrote:
>I was just trying to check if objects were the same (object), didn't
know Integers were a special case.

they're not, really; "is" works the same way for all objects.

when you ask for a new immutable object, a Python implementation may
always reuse an existing object, if it wants to.

the CPython implementation does this for small integers, booleans, None,
empty tuples, certain strings, type objects, etc. this also applies to
library code; for example, string methods often return a reference to
"self" if the method didn't actually change anything.
I find this a bit oddly worded. Now the "may always reuse" phrase
suggests this is not an obligation and I can certainly understand
that in the case of integers. But when you enumerate examples you
include None and Booleans, creating the suggestion these don't
have to be implemented as singletons either and that there can be
more than one None, True and False object. Now I probably just
misunderstand, but I'm wondering, is there somewhere in the language
reference that specifies these have to be singletons?

--
Antoon Pardon
Sep 26 '06 #9

Antoon Pardon wrote:
>
I find this a bit oddly worded. Now the "may always reuse" phrase
suggests this is not an obligation and I can certainly understand
that in the case of integers. But when you enumerate examples you
include None and Booleans, creating the suggestion these don't
have to be implemented as singletons either and that there can be
more than one None, True and False object. Now I probably just
misunderstand, but I'm wondering, is there somewhere in the language
reference that specifies these have to be singletons?
Yes. The topic "The standard Type Hierarchy" in the
Language reference specifies this exactly.

John Roth
>
--
Antoon Pardon
Sep 26 '06 #10
On 2006-09-26, John Roth <Jo*******@jhrothjr.comwrote:
>
Antoon Pardon wrote:
>>
I find this a bit oddly worded. Now the "may always reuse" phrase
suggests this is not an obligation and I can certainly understand
that in the case of integers. But when you enumerate examples you
include None and Booleans, creating the suggestion these don't
have to be implemented as singletons either and that there can be
more than one None, True and False object. Now I probably just
misunderstand, but I'm wondering, is there somewhere in the language
reference that specifies these have to be singletons?

Yes. The topic "The standard Type Hierarchy" in the
Language reference specifies this exactly.
Thank you very much.

--
Antoon Pardon
Sep 26 '06 #11

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

Similar topics

7
by: John Hunter | last post by:
I am using pycxx 5.2.2 to generate some extension code. I want to extract some doubles from some python sequences When I do double l( Py::Float(rect) ); double b( Py::Float(rect) ); and...
14
by: Fabian | last post by:
function getImage(id,x,y) { if (mpdef == undefined) { // set to default image document.getElementById(id).src = terrdef; } else { document.getElementById(id).src = terrdef ]; } return; }
3
by: Leith Bade | last post by:
I have been trying to use the new Visual C++ Toolkit 2003 with the VC6 IDE I set up the executable, inlcude, and library directories to point to the new compilers I had to fix a few errors in the...
10
by: Tony Young | last post by:
Hi, I have an array of integer. I need to find the max of the 0th, 3th, 6th , 9th, ... elements in the array. I wonder if STL (standard template library) provides a way to manipulate only part...
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
0
by: santana | last post by:
Hi I've created a class to be used with stl vector. I'm having a hard time decipher the error message outpout by g++... burlen@quaoar:~/hdf52vtk_dev/src$ g++ junk.cpp GridPoint.o...
5
by: meng.frank | last post by:
If I want to use std::vector or std::list for my class MyNewClass, which operators should I override? Maybe operator new and operator delete? If I want to use MyNewClass in std::set or std::map,...
9
by: Matthias Pospiech | last post by:
I have a Class for complex operations: class CComplex { public: inline CComplex() { re=im=0.0; } inline CComplex operator*(CComplex c) { return CComplex(re*c.re-im*c.im,re*c.im+im*c.re);}...
7
by: yodadbl07 | last post by:
hey im trying to write a class of polynomials using a linked list. But I am getting an error at run time with my Polynomial* getNext() function. The error says access violation reading location. Im...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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,...
0
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...

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.