string identity and comparison

Arnaud Delobelle arnodel at gmail.com
Thu Dec 16 16:15:28 EST 2010


Jean-Michel Pichavant <jeanmichel at sequans.com> writes:

> 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 ?
>
> JM

Have you tried this?

>>> id("foo")
3078219968L
>>> id("bar")
3078219968L
>>> 

And this?

>>> id("foo")
3077306560L
>>> n = 42
>>> id("foo")
3077306720L

And this?

>>> "a"*2 is "a"*2
True
>>> "a"*30 is "a"*30
False
>>> n = 2
>>> "a"*n is "a"*n
False

-- 
Arnaud



More information about the Python-list mailing list