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
aiqier
V2EX  ›  Python

关于 python 大整数对象池

  •  
  •   aiqier · 2016-12-10 11:23:04 +08:00 · 3018 次点击
    这是一个创建于 2698 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

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

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

    我也在看。不过很奇怪的是,这段代码里并没有根据值去寻找已存在的大整数。所以按照代码来看应该是就算是数值
    相同的两个大整数,应该是每次都返回不同的地址才对。
    glogo
        3
    glogo  
       2016-12-10 17:12:27 +08:00
    这个问题应该是楼主问的吧, https://segmentfault.com/q/1010000007756407
    答案 R 大给出了一个
    aiqier
        4
    aiqier  
    OP
       2016-12-10 18:07:58 +08:00
    @glogo 对,是我问的,。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2524 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 13:18 · PVG 21:18 · LAX 06:18 · JFK 09:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.