string identity and comparison

  • Thread starter Jean-Michel Pichavant
  • Start date
J

Jean-Michel Pichavant

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
 
B

bruno.desthuilliers

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 ?

2 points:


1- an id is only valid for the lifetime of a given object - when the
object has been collected, the id can be reused for another object.

2- in CPython, strings that would be valid Python identifiers are
interned. Try using a string that would not be a valid Python
identifier

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.

HTH
 
M

Mel

Jean-Michel Pichavant said:
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@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.True


Mel.
 
J

Jean-Michel Pichavant

2 points:


1- an id is only valid for the lifetime of a given object - when the
object has been collected, the id can be reused for another object.
that's exactly what happened in my example, the 2 'foo' are not the same
object actually, the fact that the 2 successive id() calls return the
same value is implementation specific.
Thanks for pointing that out, I didn't realize in the first place.

JM
 
J

Jean-Michel Pichavant

Mel said:
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@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.
3075373248L

3075373856L
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
 
B

bruno.desthuilliers

that's exactly what happened in my example, the 2 'foo' are not the same
object actually, the fact that the 2 successive id() calls return the
same value is implementation specific.
Thanks for pointing that out, I didn't realize in the first place.

been here, done that :-/
 
B

bruno.desthuilliers

Mel said:
Jean-Michel Pichavant wrote:
Currently, CPython interns strings that look like identifiers.  Any strings
that don't look like identifiers are on their own:
mwilson@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
3075373856L

   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.

Which FWIW is one more reason to avoid identity testing on strings -
too much implementation specific stuff happening here.
 
A

Arnaud Delobelle

Jean-Michel Pichavant said:
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?

And this?
3077306720L

And this?
False
 
A

alex23

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.

Anyone has that kind of code ?

It's quite obvious when they come from different contexts:
False
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top