string identity and comparison

Mel mwilson at the-wire.com
Thu Dec 16 08:50:36 EST 2010


Jean-Michel Pichavant wrote:

> Fellows,
> 
> I'd like to illutrate the fact that comparing strings using identity is,
> most of the time, a bad idea. However I'm searching a short example of
> code that yields 2 differents object for the same string content.
> 
> id('foo')
> 3082385472L
> id('foo')
> 3082385472L
> 
> Anyone has that kind of code ?

Currently, CPython interns strings that look like identifiers.  Any strings 
that don't look like identifiers are on their own:

mwilson at tecumseth:~/sandbox/candlekit/stringlight-1$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 'x(3)'
>>> id(a)
3075373248L
>>> c='x(3)'
>>> id(c)
3075373856L
>>> a==c
True


	Mel.



More information about the Python-list mailing list