string identity and comparison
Jean-Michel Pichavant
jeanmichel at sequans.com
Thu Dec 16 09:53:19 EST 2010
Mel wrote:
> 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.
>
thanks to all who replied.
It looks like there are some differences between python 2.5 & 2.6, I
tested all the possibilities I've been given in this thread and did not
always get the same result.
Anyway I found what I was looking for.
JM
More information about the Python-list
mailing list