Hugo González Monteverde wrote: >> Hmmm! Hmmm! >> Lookee here: >> ## console session >> >>> a=[1,2] >> >>> b=[1,2] >> >>> a is b >> False >> >>> c='1' ## one byte >> >>> d='1' ## one byte >> >>> c is d >> True >> >>> c='1,2' >> >>> d='1,2' >> >>> c is d >> False >> >> The Hmmm! is emmitted because I'm thinking that if everything is an >> object in python, then why does `c is d` evaluate to True when >> the assigned value is 1 byte and evaluate to False when the assigned >> value is more that 1 byte? > > One and two byte strings are currently optimized in cPython as the same > object, referenced multiple times.
It's not just one-byte strings; I think any string that can be a Python identifier is interned and will always be the same string. In [1]: a='abcd' In [2]: b='abcd' In [3]: a is b Out[3]: True > > Note that this is not to be relied upon! Jython, Ironpython, Python3000 > or cPython itself may break it! Right. But why do you even care? I don't think I have ever written code that compares strings using 'is'; just use ==. > > Still cannot find a reference doc for this.... Probably the source code will be the only reference, this is an implementation detail. Kent _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor