我知道有人在我创建时解释了为什么 Python 2.7 中的相等 unicode 字符串 它们不指向内存中的相同位置 与“普通”字符串一样
>>> a1 = 'a'
>>> a2 = 'a'
>>> a1 is a2
True
好吧,这正是我所期望的,但是
>>> ua1 = u'a'
>>> ua2 = u'a'
>>> ua1 is ua2
False
为什么?怎么办?
最佳答案
我认为常规字符串是 intern 的,但 unicode 字符串不是。这个简单的测试似乎支持我的理论(Python 2.6.6):
>>> intern("string")
'string'
>>> intern(u"unicode string")
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
intern(u"unicode string")
TypeError: intern() argument 1 must be string, not unicode
关于python - unicode 字符串中的内存位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15393893/