On Jun 19, 2:26 am, Faheem Mitha <[EMAIL PROTECTED]> 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 [1]: a = '--' > > In [2]: a is '--' > Out[2]: False > > In [4]: a = '-' > > In [5]: a is '-' > Out[5]: True > > In [6]: a = 'foo' > > In [7]: a is 'foo' > Out[7]: 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. -- http://mail.python.org/mailman/listinfo/python-list