[ python-Bugs-1086096 ] two strings holding the same value have different id

SourceForge.net noreply at sourceforge.net
Thu Dec 16 00:14:29 CET 2004


Bugs item #1086096, was opened at 2004-12-15 17:10
Message generated for change (Comment added) made by goodger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1086096&group_id=5470

Category: Python Interpreter Core
Group: Python 2.4
>Status: Closed
>Resolution: Rejected
Priority: 5
Submitted By: Stefan Seefeld (stefan)
Assigned to: Nobody/Anonymous (nobody)
Summary: two strings holding the same value have different id

Initial Comment:
I'v run into a problem with a program that expects a string
to be either 'resource' or 'test':

type = ...

if type is "resource":
   do_something()
elif type is "test":
   do_something_else()


The above comparison using 'is' works fine with python 2.3,
but fails with python 2.4. Unfortunately I wasn't able to
isolate the problem. 

To reproduce:

Install qmtest from http://www.qmtest.com on windows xp
with python 2.4 (I don't know yet whether other platforms
are affected, too).
Then follow the instructions from
http://www.codesourcery.com/public/qmtest/qm-2.2/manual.html
up to section 2.2 'Starting the Graphical Interface'.
In the browser, click on the 'exec1' test, and you'll
get an
'UnboundLocalError' because the application logic didn't 
expect some comparison to fail.
The failed comparison in question is 't is "test"',
which returns 'False' even though t == "test".



----------------------------------------------------------------------

>Comment By: David Goodger (goodger)
Date: 2004-12-15 18:14

Message:
Logged In: YES 
user_id=7733

I fully agree with Tim.  If the example code worked before,
it was accidental.  "is" tests identity.  ``"resource" is
"resource"`` may create two independent string objects.  Use
"==" instead.

Closed the bug report.

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2004-12-15 17:27

Message:
Logged In: YES 
user_id=31435

Sorry, the only bug I see here is in the code you posted 
using "is" to try to determine whether two strings are 
equal.  "is" tests for object identity, not for equality, and 
whether two immutable objects are really the same object 
isn't in general defined by Python.  You should use "==" to 
check two strings for equality.  The only time it's reliable to 
use "is" for that purpose is when you've explicitly interned all 
strings being compared (via using the intern() builtin function).

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1086096&group_id=5470


More information about the Python-bugs-list mailing list