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

函数id()这货有什么用?

  •  
  •   ThunderEX · 2013-03-01 16:54:56 +08:00 · 3408 次点击
    这是一个创建于 4072 天前的主题,其中的信息可能已经有所发展或是发生改变。
    CPython说是返回的是内存里的地址,int值,这玩意有用么?
    5 条回复    1970-01-01 08:00:00 +08:00
    yishenggudou
        1
    yishenggudou  
       2013-03-01 19:02:02 +08:00
    比较对象
    ThunderEX
        2
    ThunderEX  
    OP
       2013-03-01 19:09:14 +08:00
    @yishenggudou 不是有is了么?
    dreampuf
        3
    dreampuf  
       2013-03-01 19:09:37 +08:00
    http://docs.python.org/2/library/functions.html#id
    http://stackoverflow.com/questions/3402679/identifying-objects-why-does-the-returned-value-from-id-change

    接着SO的链接

    In [1]: a = 5

    In [2]: id(a)
    Out[2]: 140615773913304

    In [3]: b = 5

    In [4]: id(b)
    Out[4]: 140615773913304

    In [5]: a = 100005

    In [6]: id(a)
    Out[6]: 140615774390544

    In [7]: b = 100005

    In [8]: id(b)
    Out[8]: 140615774253864
    phuslu
        4
    phuslu  
       2013-03-01 19:09:53 +08:00 via iPhone
    的确没啥用,目前我用到的地方也就是 id(gevent.getcurrent()) 的返回值「充当」 thread id用。
    ThunderEX
        5
    ThunderEX  
    OP
       2013-03-01 19:39:58 +08:00
    @dreampuf 所以……不是用is就够了么……
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1972 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:18 · PVG 00:18 · LAX 09:18 · JFK 12:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.