python - unicode 字符串中的内存位置

标签 python unicode python-2.7

我知道有人在我创建时解释了为什么 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/

相关文章:

python - 自动从 HTML 页面发送和接收数据

r - 从字符串中删除 Unicode 替换字符

python - Python Simple-Salesforce exe 的 SSL 错误

c - UNICODE 和 _UNICODE 之间的区别

unicode - UTF-8编码为什么前缀10?

python - 从 Python 列表中删除列表子集的最快方法

django - 在 Django REST Framework 的同一个 View 上使用 ViewSet 和 APIView mixins 是否正确?

python - Django:如何在 Wagtail CMS 中使用两个不同的数据库

python - 从字典值中删除外部列表

python - 在 Python 中处理惰性 JSON - 'Expecting property name'