Issue 1086096: two strings holding the same value have different id (original) (raw)

Issue1086096

Created on 2004-12-15 22:10 by stefan, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg23719 - (view) Author: Stefan Seefeld (stefan) * (Python committer) Date: 2004-12-15 22:10
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".
msg23720 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2004-12-15 22:27
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).
msg23721 - (view) Author: David Goodger (goodger) (Python committer) Date: 2004-12-15 23:14
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.
History
Date User Action Args
2022-04-11 14:56:08 admin set github: 41329
2004-12-15 22:10:01 stefan create