python string comparison oddity
Lie.1296 at gmail.com
Thu Jun 19 12:14:30 CEST 2008
On Jun 19, 5:13 am, Faheem Mitha <fah... at email.unc.edu> wrote:
> On Wed, 18 Jun 2008 12:57:44 -0700 (PDT), Lie <Lie.1... at gmail.com> wrote:
> > On Jun 19, 2:26 am, Faheem Mitha <fah... at email.unc.edu> wrote:
> >> Hi everybody,
> >> I was wondering if anyone can explain this. My understanding is that 'is'
> >> checks if the object is the same. However, in that case, why this
> >> inconsistency for short strings? I would expect a 'False' for all three
> >> comparisons. This is reproducible across two different machines, so it is
> >> not just a local quirk. I'm running Debian etch with Python 2.4.4 (the
> >> default).
> >> Thanks, Faheem.
> >> In : a = '--'
> >> In : a is '--'
> >> Out: False
> >> In : a = '-'
> >> In : a is '-'
> >> Out: True
> >> In : a = 'foo'
> >> In : a is 'foo'
> >> Out: True
> > Yes, this happens because of small objects caching. When small
> > integers or short strings are created, there are possibility that they
> > might refer to the same objects behind-the-scene. Don't rely on this
> > behavior.
> Yes, but why is '-' and 'foo' cached, and not '--'? Do you know what
> the basis of the choice is?
Yes, but we're already warned not to rely on it since the basis of
what may be cached and what-not might be arbitrary. Personally, I'd
not delve deeply into them, they aren't a reliable behavior.
More information about the Python-list