From: Bruno Desthuilliers on
Richard D. Moores a écrit :
> On Thu, Aug 5, 2010 at 16:15, Rhodri James <rhodri(a)wildebst.demon.co.uk> wrote:
>> On Thu, 05 Aug 2010 17:07:53 +0100, wheres pythonmonks
>> <wherespythonmonks(a)gmail.com> wrote:
>
>> You're not testing for equivalence there, you're testing for identity. "is"
>> and "is not" test whether the two objects concerned are (or are not) the
>> same object. Two objects can have the same value, but be different objects.
>> The interpreter can fool you by caching and reusing objects which have the
>> same value when it happens to know about it, in particular for small
>> integers, but this is just a happy accident of the implementation and in no
>> way guaranteed by the language. For example:
>>
>>>>> "spam, eggs, chips and spam" is "spam, eggs, chips and spam"
>> True
>>>>> a = "spam, eggs, chips and spam"
>>>>> b = "spam, eggs, chips and spam"
>>>>> a is b
>> False
>>>>> a == b
>> True
>>
>
> I'm wondering if there isn't considerable predictability to that
> "happy accident".

There is, indeed, but that's really implementation details.

> Note how 1 'word' is treated versus 2:
>
>>>> x = 'alksjdhflkajshdflkajhdflkjahsdflkjshadflkjhsadlfkjhaslkdjfhslkadhflkjshdflkjshdflkjshdfk'
>>>> y = 'alksjdhflkajshdflkajhdflkjahsdflkjshadflkjhsadlfkjhaslkdjfhslkadhflkjshdflkjshdflkjshdfk'
>>>> x is y
> True
>>>> x = 'alksjdhflkajshdflkajhdflkjahsdflkj hadflkjhsadlfkjhaslkdjfhslkadhflkjshdflkjshdflkjshdfk'
>>>> y = 'alksjdhflkajshdflkajhdflkjahsdflkj hadflkjhsadlfkjhaslkdjfhslkadhflkjshdflkjshdflkjshdfk'
>>>> x is y
> False


CPython caches strings that happen to be valid Python identifiers. But
once again, this is an implementation-specific optimization.

From: Jean-Michel Pichavant on
Steven D'Aprano wrote:
> On Thu, 05 Aug 2010 12:07:53 -0400, wheres pythonmonks wrote:
>
>
>> P.S. Sorry for the top-post -- is there a way to not do top posts from
>> gmail? I haven't used usenet since tin.
>>
>
> Er, surely you can just move the cursor before you start typing???
>
>
>
>
CTRL+END will bring the cursor at the end of the file (for most the the
mail clients).


JM
From: Roald de Vries on
On Aug 6, 2010, at 9:25 AM, Bruno Desthuilliers wrote:
> Roald de Vries a écrit :
>> 'not None' first casts None to a bool, and then applies 'not', so
>> 'x is not None' means 'x is True'.
>
> Obviously plain wrong :
>
> Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
> [GCC 4.3.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> 42 is not None
> True
> >>> 42 is True
> False
> >>>

I know. I'm sorry. I'm embarrassed. I will check my facts better
before posting in the future. And I'm sorry.

Cheers, Roald

From: Vito 'ZeD' De Tullio on
Richard D. Moores wrote:

> So there would be a different implementation
for each operating
> system? One for Windows, one for linux? Or one for
Vista and one for
> XP? I'm just trying to clarify what is meant by
"implementation".

there are dozillions of "implementation" of python: one
for each release of CPython (2.0, 2.1.2, 2.6.1, 3.x.y...), multiplied for
each OS multiplied for IronPython, Jython, Pypy... etc...

(obviously the
"implementation details" between, say, CPython 2.6.1 and 2.6.2 are really
minor vs Pypy X.Y.Z and IronPython A.B.C)

--
By ZeD


From: DG on
On Aug 6, 2:32 am, Bruno Desthuilliers <bruno.
42.desthuilli...(a)websiteburo.invalid> wrote:
> Richard D. Moores a écrit :
>
>
>
> > On Thu, Aug 5, 2010 at 16:15, Rhodri James <rho...(a)wildebst.demon.co.uk> wrote:
> >> On Thu, 05 Aug 2010 17:07:53 +0100, wheres pythonmonks
> >> <wherespythonmo...(a)gmail.com> wrote:
>
> >> You're not testing for equivalence there, you're testing for identity.  "is"
> >> and "is not" test whether the two objects concerned are (or are not) the
> >> same object.  Two objects can have the same value, but be different objects.
> >>  The interpreter can fool you by caching and reusing objects which have the
> >> same value when it happens to know about it, in particular for small
> >> integers, but this is just a happy accident of the implementation and in no
> >> way guaranteed by the language.  For example:
>
> >>>>> "spam, eggs, chips and spam" is "spam, eggs, chips and spam"
> >> True
> >>>>> a = "spam, eggs, chips and spam"
> >>>>> b = "spam, eggs, chips and spam"
> >>>>> a is b
> >> False
> >>>>> a == b
> >> True
>
> > I'm wondering if there isn't considerable predictability to that
> > "happy accident".
>
> There is, indeed, but that's really implementation details.
>
> > Note how 1 'word' is treated versus 2:
>
> >>>> x = 'alksjdhflkajshdflkajhdflkjahsdflkjshadflkjhsadlfkjhaslkdjfhslkadhflkjshdflkjshdflkjshdfk'
> >>>> y = 'alksjdhflkajshdflkajhdflkjahsdflkjshadflkjhsadlfkjhaslkdjfhslkadhflkjshdflkjshdflkjshdfk'
> >>>> x is y
> > True
> >>>> x = 'alksjdhflkajshdflkajhdflkjahsdflkj hadflkjhsadlfkjhaslkdjfhslkadhflkjshdflkjshdflkjshdfk'
> >>>> y = 'alksjdhflkajshdflkajhdflkjahsdflkj hadflkjhsadlfkjhaslkdjfhslkadhflkjshdflkjshdflkjshdfk'
> >>>> x is y
> > False
>
> CPython caches strings that happen to be valid Python identifiers. But
> once again, this is an implementation-specific optimization.

I've always thought of it as you don't compare strings with "is", you
*should* use == The reasoning is that you don't know if that string
instance is the only one in memory. I've heard as an implementation
detail, since strings are immutable, that Python will only create one
actual instance and just assign new references to it (your first x is
y example), but to compare equality it just makes sense to use "==",
not to ask if it is the same object in memory. Plus, I believe the
"==" operator will check if the variables point to the same object.

Using is/is not with None works well, because I believe there will
always only be 1 None object.