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

有个疑惑

  •  
  •   bxb100 · 2017-01-21 11:21:45 +08:00 · 2857 次点击
    这是一个创建于 2623 天前的主题,其中的信息可能已经有所发展或是发生改变。
    python 中 id([1]) == id([2]) 为 True,这是为啥啊
    7 条回复    2017-01-21 19:20:03 +08:00
    czheo
        1
    czheo  
       2017-01-21 11:28:23 +08:00   ❤️ 1
    性能优化,内部把 list 重用了吧。
    imn1
        2
    imn1  
       2017-01-21 11:31:24 +08:00
    id([1]) is id([2])
    bxb100
        3
    bxb100  
    OP
       2017-01-21 11:43:59 +08:00 via Android
    @czheo tuple 为啥不会出现这种情况
    czheo
        4
    czheo  
       2017-01-21 11:46:26 +08:00
    tuple 是 immutable 的,没法重用
    bxb100
        5
    bxb100  
    OP
       2017-01-21 11:52:53 +08:00 via Android
    @czheo id(()) == id(())返回的为 True
    bxb100
        6
    bxb100  
    OP
       2017-01-21 13:32:19 +08:00 via Android
    @czheo list 是临时对象,内存会自动清理, tuple 不会,而且 ()和(1)存放地址区域不同
    HypoChen
        7
    HypoChen  
       2017-01-21 19:20:03 +08:00   ❤️ 1
    id(object)

    Return the “ identity ” of an object. This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime. **Two objects with non-overlapping lifetimes may have the same id() value.**

    ```
    >>> id([1])
    4391579520
    >>> id([2])
    4391579520
    >>> a = [1]
    >>> b = [1]
    >>> id([1])
    4391757944
    >>> id([2])
    4391757944
    >>> id(a)
    4391579520
    >>> id(b)
    4391641672
    >>> id([])
    4391757944
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1649 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:47 · PVG 00:47 · LAX 09:47 · JFK 12:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.