Strange behaviour of 'is'

Steve Holden steve at holdenweb.com
Fri Sep 22 08:28:32 EDT 2006


Duncan Booth wrote:
> Steve Holden <steve at holdenweb.com> wrote:
> 
> 
>>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




More information about the Python-list mailing list