On Thu, 11 Apr 2013, Timo wrote:

Op 11-04-13 12:41, w qj schreef:
I found this under Windows Python3
l="http://f/";
l[-1] is not '/'
False
and this under Linux Python3
l = "http://ff.f/";
l[-1]
'/'
l[-1] is not '/'
True

It's Looks like a python bug?
This looks like a "is not" versus "!=" thing. Someone (I think Steven Apprano) posted a couple of days ago on this mailing list to only use "is" and "is not" when comparing to None.

You're absolutely correct.

`is` compares the identity (basically id('/') == id('/') )

*sometimes* CPython will use a trick that caches smaller strings, so this might work one time:

    >>> x = '/'
    >>> y = '/'
    >>> x is y
    True

But then the next time you do the same thing it could return False. Or on a different OS. There's nothing (that I'm aware of) that will guarantee either result in this case.

In the example case we were comparing that `l[-1]` referred to the same spot in memory as the literal string `/`. And as the example showed, sometimes it will be, other times it won't.


The takeaway is to use `is` when you want to compare identity, and `==` when you want equaltiy. For example,

   That car is *my* car.

The car I'm referring to is one specific car. But if I were to say...

   My car is a Chevette.

That would be more like saying `car.model == 'Chevette'` - many different cars may actually be a Chevette.

HTH,
Wayne
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to