473,385 Members | 1,379 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.

Strange behaviour of 'is'

Hi all,

I am a bit perplexed by the following behaviour of the 'is' comparator
>>x = 2.
x is 2.
False
>>y = [2., 2.]
y[0] is y[1]
True

My understanding was that every literal is a constructure of an object.
Thus, the '2.' in 'x = 2.' and the '2.' in 'x is 2.' are different objects.
Therefore, the comparison yields false.

But my understanding does not explain the result of the second comparison.
According to the experiment, y[0] and y[1] are the same object!

Does anyone know an explanation for this?

Thank you very much
Fijoy
Sep 21 '06 #1
9 1198
"Fijoy George" <to*****@yahoo.co.inwrote:
My understanding was that every literal is a constructure of an
object. Thus, the '2.' in 'x = 2.' and the '2.' in 'x is 2.' are
different objects. Therefore, the comparison yields false.
What gave you that idea? The compiler may or may not fold together
identical immutable objects when it compiles your code. In your first
example the comparison is False because the two lines are compiled
separately. If compiled together it might have returned True:
>>x = 2.; x is 2.
True

However this is purely an implementation detail. The language doesn't
actually require either behaviour.

Sep 21 '06 #2
On 2006-09-21, Fijoy George <to*****@yahoo.co.inwrote:
Hi all,

I am a bit perplexed by the following behaviour of the 'is' comparator
>>>x = 2.
x is 2.
False
>>>y = [2., 2.]
y[0] is y[1]
True

My understanding was that every literal is a constructure of an object.
Thus, the '2.' in 'x = 2.' and the '2.' in 'x is 2.' are different objects.
Therefore, the comparison yields false.

But my understanding does not explain the result of the second comparison.
According to the experiment, y[0] and y[1] are the same object!
I'm as baffled as you, even more so its implication:
>>a = 2.
b = 2.
>>a is b
False
>>a, b = 2., 2.
a is b
True
Sep 21 '06 #3
Ben C wrote:
On 2006-09-21, Fijoy George <to*****@yahoo.co.inwrote:
But my understanding does not explain the result of the second comparison.
According to the experiment, y[0] and y[1] are the same object!

I'm as baffled as you, even more so its implication:
>a = 2.
b = 2.
>a is b
False
>a, b = 2., 2.
a is b
True
It is suprising that it is easier to recognize identical constants
within the same expression?

-Mike

Sep 21 '06 #4
Ben C wrote:
I'm as baffled as you, even more so its implication:
There is no implication whatsoever. Whether immutable objects are
recycled by the compiler or not is totally implementation dependent, and
an irrelevant implementation detail. Since they can't be mutated,
whether their identity is shared with other objects in other contexts is
never significant.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
Little things / Cut like knives / Hurt and sting
-- Anggun
Sep 21 '06 #5
"Fijoy George" <to*****@yahoo.co.inwrites:
I am a bit perplexed by the following behaviour of the 'is' comparator
In summary: you are seeing implementation details that the language
specification explicitly allows to vary by implementation.
My understanding was that every literal is a constructure of an
object. Thus, the '2.' in 'x = 2.' and the '2.' in 'x is 2.' are
different objects. Therefore, the comparison yields false.
There's no "thus" about it. Any implementation may use the same object
or different objects to represent any two values that are equal. This
allows different optimisations to be performed on different platforms,
without breaking the specified behaviour.
But my understanding does not explain the result of the second
comparison. According to the experiment, y[0] and y[1] are the same
object!

Does anyone know an explanation for this?
The explanation is:

Two objects compare *equal* if their '__eq__' method says they are. In
practice, this means their *values* are the same.

Two objects compare *identical* if the 'id()' function says they
are. In practice, this means they are the *same object*.

The relationship between "same object" and "same value" is entirely up
to the implementation to decide, by any simple or complex criteria it
chooses, and there's nothing in the specification that requires it to
be consistent at the program level. This is, among other reasons, to
allow optimisations that don't change the language specification.

No general promise is made about the relationship between those two
comparisons. Don't use them as if they were the same operation,
because the implementation isn't restricted to meet that promise.

--
\ "You know what I hate? Indian givers... no, I take that back." |
`\ -- Emo Philips |
_o__) |
Ben Finney

Sep 21 '06 #6
Ben Finney wrote:
"Fijoy George" <to*****@yahoo.co.inwrites:

>>I am a bit perplexed by the following behaviour of the 'is' comparator


In summary: you are seeing implementation details that the language
specification explicitly allows to vary by implementation.

>>My understanding was that every literal is a constructure of an
object. Thus, the '2.' in 'x = 2.' and the '2.' in 'x is 2.' are
different objects. Therefore, the comparison yields false.


There's no "thus" about it. Any implementation may use the same object
or different objects to represent any two values that are equal. This
allows different optimisations to be performed on different platforms,
without breaking the specified behaviour.

>>But my understanding does not explain the result of the second
comparison. According to the experiment, y[0] and y[1] are the same
object!

Does anyone know an explanation for this?


The explanation is:

Two objects compare *equal* if their '__eq__' method says they are. In
practice, this means their *values* are the same.

Two objects compare *identical* if the 'id()' function says they
are. In practice, this means they are the *same object*.

The relationship between "same object" and "same value" is entirely up
to the implementation to decide, by any simple or complex criteria it
chooses, and there's nothing in the specification that requires it to
be consistent at the program level. This is, among other reasons, to
allow optimisations that don't change the language specification.

No general promise is made about the relationship between those two
comparisons. Don't use them as if they were the same operation,
because the implementation isn't restricted to meet that promise.
Absolutely correct. It would be more interesting to discuss how the
output from these statements varied between (say) CPython, Jython and
Iron Python. At the moment the discussion is indeed about insignificant
implementation trivia.

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

Sep 22 '06 #7
Steve Holden <st***@holdenweb.comwrote:
Absolutely correct. It would be more interesting to discuss how the
output from these statements varied between (say) CPython, Jython and
Iron Python. At the moment the discussion is indeed about insignificant
implementation trivia.
CPython seems to collapse identical float values if they are in the same
compilation unit:
>>x = 2.
y = 2.
x is y
False
>>x = 2.; y = 2.
x is y
True
>>y = [2., 2.]
y[0] is y[1]
True

IronPython doesn't collapse them even when they are in expression:

IronPython 1.0.60816 on .NET 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
>>x = 2.
y = 2.
x is y
False
>>x = 2.; y = 2.
x is y
False
>>y = [2., 2.]
y[0] is y[1]
False

JPython seems to behave in a similar manner to CPython:

Python command console - JPython 2.1
>>x = 2.
y = 2.
x is y
0
>>x = 2.; y = 2.
x is y
1
>>y = [2., 2.]
y[0] is y[1]
1
>>>
Sorry, I don't have a more recent Jython implementation to hand to complete
the comparison.
Sep 22 '06 #8
Duncan Booth wrote:
Steve Holden <st***@holdenweb.comwrote:

>>Absolutely correct. It would be more interesting to discuss how the
output from these statements varied between (say) CPython, Jython and
Iron Python. At the moment the discussion is indeed about insignificant
implementation trivia.


CPython seems to collapse identical float values if they are in the same
compilation unit:

>>>>x = 2.
y = 2.
x is y

False
>>>>x = 2.; y = 2.
x is y

True
>>>>y = [2., 2.]
y[0] is y[1]

True

IronPython doesn't collapse them even when they are in expression:

IronPython 1.0.60816 on .NET 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
>>>>x = 2.
y = 2.
x is y

False
>>>>x = 2.; y = 2.
x is y

False
>>>>y = [2., 2.]
y[0] is y[1]

False

JPython seems to behave in a similar manner to CPython:

Python command console - JPython 2.1

>>>>x = 2.
y = 2.
x is y

0

>>>>x = 2.; y = 2.
x is y

1

>>>>y = [2., 2.]
y[0] is y[1]

1

Sorry, I don't have a more recent Jython implementation to hand to complete
the comparison.
Perfectly all right: you make the point very well that the behavior is
an implementation artifact and not a language feature.

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

Sep 22 '06 #9
Steve Holden <st***@holdenweb.comwrites:
Perfectly all right: you make the point very well that the behavior is
an implementation artifact and not a language feature.
And for those who may be thinking "oh, so I can at least depend on the
behaviour within a particular implementation", that's wrong too:
implementations are free to (and most will) change that behaviour from
one object or assignment to the next, depending on what optimisation
choices are made.

--
\ "He who allows oppression, shares the crime." -- Erasmus |
`\ Darwin, grandfather of Charles Darwin |
_o__) |
Ben Finney

Sep 23 '06 #10

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

Similar topics

0
by: Frank Passek | last post by:
Dear all, I've encountered some strange behaviour with PHP (4.3.2) using the CLI-API. When I provide an option in the first line of my script like so: #!/usr/bin/php -c /path_to_my_ini_file and...
2
by: Markus Franz | last post by:
Hi. Today I created a script called load.py for using at the command line written in Python 2.3. This script should load as many websites as given on the comand line and print them with a...
2
by: Paul Drummond | last post by:
Hi all, I am developing software for Linux Redhat9 and I have noticed some very strange behaviour when throwing exceptions within a shared library. All our exceptions are derived from...
3
by: Sebastian C. | last post by:
Hello everybody Since I upgraded my Office XP Professional to SP3 I got strange behaviour. Pieces of code which works for 3 years now are suddenly stop to work properly. I have Office XP...
6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
9
by: luke | last post by:
Hi everybody, please, can someone explain me this behaviour. I have the following piece of code: long long ll; unsigned int i = 2; ll = -1 * i; printf("%lld\n", ll);
5
by: Praveen_db2 | last post by:
Hi All db2 8.1.3 Windows I have folowing table structures CREATE TABLE tb_RTB( EMP_ID INTEGER, DESC VARCHAR(20)); CREATE TABLE tb_ERROR( SQL_STATE CHAR(5), SQL_DESC VARCHAR(20),
1
by: Sek | last post by:
Hi Folks, I have come across a behaviour which seems strange to me. I wrote the following code for a task: -----BEGIN CODE------- XmlDocument idc = new XmlDocument();...
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
20
by: Pilcrow | last post by:
This behavior seems very strange to me, but I imagine that someone will be able to 'explain' it in terms of the famous C standard. -------------------- code -----------------------------------...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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...

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.