V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
RitchieLee
V2EX  ›  Python

关于 CPython 中字符串 interning 的问题

  •  
  •   RitchieLee · 2019-05-24 11:44:34 +08:00 · 1390 次点击
    这是一个创建于 1771 天前的主题,其中的信息可能已经有所发展或是发生改变。

    小弟今天对 interning 还有 Python 中 str 类型的实现比较感兴趣

    现在有一个问题,就是我看到了 PyUnicodeObject 这个相关的文件有一个函数 PyUnicode_InternInPlace,我看了下这个函数主要应该是做 interning 的,但是是否我们平常赋值 str 类型时就会调用这个函数呢?

    还有一个点我也不太确定,就是 str 类型是不是在现阶段 (python3.3 之后) 底层实现就是 PyUnicdoeObject 了呢,这个我也暂时没有找到依据。看到了一个 pep 文件

    PyUnicodeObject 主要内容在 Include/unicodeobject.hObjects/unicodeobject.c 两个文件

    然后 PyUnicode_InternInPlace 定义在这里

    3 条回复    2019-05-24 14:13:58 +08:00
    lrxiao
        1
    lrxiao  
       2019-05-24 12:08:22 +08:00   ❤️ 1
    http://guilload.com/python-string-interning/ 应该只有 compile-time 的字符串才会 intern
    https://docs.python.org/3.0/whatsnew/3.0.html py3 默认 Unicode string 作为 str
    RitchieLee
        2
    RitchieLee  
    OP
       2019-05-24 12:50:30 +08:00
    @lrxiao 感谢,第二个链接我详细看看
    如果过是交互器解释器的话,写在一行应该也会 interning,我之前看了这篇文章: https://www.codementor.io/satwikkansal/do-you-really-think-you-know-strings-in-python-fnxh8mtha
    比如:
    a,b="wtf!","wtf!"
    a is b # True
    lrxiao
        3
    lrxiao  
       2019-05-24 14:13:58 +08:00
    @RitchieLee REPL 和普通 Py 程序应该都是要过同样的 compile 阶段的吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5369 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 07:57 · PVG 15:57 · LAX 00:57 · JFK 03:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.