473,761 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1213
"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***@holdenwe b.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***@holdenwe b.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
implementatio n 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***@holdenwe b.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
2065
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 call the script from the (bash-)commandline, PHP seems to ignore this setting. Calling the script with: php -c /path_to_my_ini_file myscript.php
2
1440
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 seperation string to stdout. The loading was done in parallel. (I used processes for this.) The script was started by the following command:
2
1973
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 std::exception. We have a base class which all processes derive from which is always instantiated in main surrounded by a try/catch(std::exception) which catches all exceptions that have not be handled at a higher level. The catch block cleans up and...
3
4874
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 Developer (SP3 for Office, SP1 for developer, JET40SP8) on Windows XP Home Edition (SP1). The same behaviour occurs on Windows 98 too.
6
2932
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 have better luck here. So here goes! My program ignores any command line arguments, or at least it's supposed to. However, when I pass any command line arguments to the program, the behaviour of one of the functions changes mysteriously. I have...
9
3952
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
3072
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
1336
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(); idc.LoadXml("<book><title>The Confidence Man</title><first-name>Herman</first-name><last-name>Melville</last-name></book>");
8
5314
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. (Sorry, long email) The first two examples are behaving normal, the thirth is strange....... I wrote the following flabbergasting code: #-------------------------------------------------------------
20
2238
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 ----------------------------------- #include <stdio.h> int main (void) { char xx="abcd"; char * p1 = xx;
0
9948
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...
1
9902
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
8770
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
7327
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
6603
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
5215
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
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
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.