推荐学习书目
› 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 · Jan 21, 2017 · 3637 views
    This topic created in 3541 days ago, the information mentioned may be changed or developed.
    python 中 id([1]) == id([2]) 为 True,这是为啥啊
    7 replies  •  2017-01-21 19:20:03 +08:00
    czheo
        1
    czheo  
       Jan 21, 2017   ❤️ 1
    性能优化,内部把 list 重用了吧。
    imn1
        2
    imn1  
       Jan 21, 2017
    id([1]) is id([2])
    bxb100
        3
    bxb100  
    OP
       Jan 21, 2017 via Android
    @czheo tuple 为啥不会出现这种情况
    czheo
        4
    czheo  
       Jan 21, 2017
    tuple 是 immutable 的,没法重用
    bxb100
        5
    bxb100  
    OP
       Jan 21, 2017 via Android
    @czheo id(()) == id(())返回的为 True
    bxb100
        6
    bxb100  
    OP
       Jan 21, 2017 via Android
    @czheo list 是临时对象,内存会自动清理, tuple 不会,而且 ()和(1)存放地址区域不同
    Hyponet
        7
    Hyponet  
       Jan 21, 2017   ❤️ 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
    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2313 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 01:23 · PVG 09:23 · LAX 18:23 · JFK 21:23
    ♥ Do have faith in what you're doing.