Is this a bug? Variables are identical references to the same string in this example (Python)

后端 未结 3 1495
说谎
说谎 2021-02-15 18:57

This is for Python 2.6.

I could not figure out why a and b are identical:

>>> a = \"some_string\"
>>> b = \"some_string\"
>>>          


        
3条回答
  •  渐次进展
    2021-02-15 19:12

    TIM PETERS SAID: 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).

    from here: http://mail.python.org/pipermail/python-bugs-list/2004-December/026772.html

提交回复
热议问题