推荐学习书目
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
aiqier
V2EX  ›  Python

关于 python 大整数对象池

  •  
  •   aiqier · Dec 10, 2016 · 3655 views
    This topic created in 3466 days ago, the information mentioned may be changed or developed.

    最近看了《 python 源码分析》,里面提到了 python 的小整数对象池和大整数对象池,然后我发现,如果我在解释器上直接运行:

    >>> a = 10000
    >>> b = 10000
    

    那么 id(a)id(b)是不一样的。

    但是如果我写个main.py去跑,那么id(a)id(b)又是一样的了,是因为交互解释器没有用到大整数对象池吗?还是因为 py 文件在编译的时候做了优化?

    4 replies    2016-12-10 18:07:58 +08:00
    zhuangzhuang1988
        1
    zhuangzhuang1988  
       Dec 10, 2016
    如果我没猜错的话, 模块里面应该是 const 值, 然后 a,b 都指向这个
    而在 repl 的时候, 每次都是创建的, repl 中不识别出 1000 是 const
    herozem
        2
    herozem  
       Dec 10, 2016
    https://github.com/jiajunhuang/cpython/blob/python_source_analysis/Objects/intobject.c#L83-L117

    我也在看。不过很奇怪的是,这段代码里并没有根据值去寻找已存在的大整数。所以按照代码来看应该是就算是数值
    相同的两个大整数,应该是每次都返回不同的地址才对。
    glogo
        3
    glogo  
       Dec 10, 2016
    这个问题应该是楼主问的吧, https://segmentfault.com/q/1010000007756407
    答案 R 大给出了一个
    aiqier
        4
    aiqier  
    OP
       Dec 10, 2016
    @glogo 对,是我问的,。。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1390 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 51ms · UTC 16:56 · PVG 00:56 · LAX 09:56 · JFK 12:56
    ♥ Do have faith in what you're doing.