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

Python 内部实现请教:用 len(l)获取普通 list 对象的大小时, Python 内部需要实时计算这个大小吗,还是对象本身有个属性就是“size”,直接读就行了?

  •  
  •   xFrank · 2019-09-11 20:04:10 +08:00 · 2757 次点击
    这是一个创建于 1687 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如题。

    9 条回复    2019-09-12 12:31:50 +08:00
    wwqgtxx
        1
    wwqgtxx  
       2019-09-11 20:10:09 +08:00   ❤️ 1
    wwqgtxx
        2
    wwqgtxx  
       2019-09-11 20:13:45 +08:00
    顺便补充一下上面用到的 Py_SIZE 的实现
    https://github.com/python/cpython/blob/master/Include/object.h#L123
    ztcaoll222
        3
    ztcaoll222  
       2019-09-11 20:23:49 +08:00
    具体看对象是怎么实现的 __len__
    list 是直接调用的 c 接口, 源码楼上有了
    xFrank
        4
    xFrank  
    OP
       2019-09-11 20:25:27 +08:00
    我们平常用的 python 都是 cpython 实现的?
    平常是指从这里下载的那种: https://www.python.org/downloads/
    或者 Linux 下用 apt install 直接装的
    wwqgtxx
        5
    wwqgtxx  
       2019-09-11 20:28:16 +08:00   ❤️ 1
    @xFrankc cpython 是 python 的官方实现,你默认下载的都是这个实现(第三方主要指 pypy jython 这类)
    xFrank
        6
    xFrank  
    OP
       2019-09-11 20:29:43 +08:00
    @wwqgtxx 好的,多谢
    smallpython
        7
    smallpython  
       2019-09-11 20:36:34 +08:00
    我的理解这些语言在设计的时候,凡是能让程序更快的设计,他都会这么做
    love
        8
    love  
       2019-09-11 21:03:27 +08:00
    这个属性用得到处都是,条件判断什么的,如果是还要计算那画面不敢想象
    ipwx
        9
    ipwx  
       2019-09-12 12:31:50 +08:00 via Android
    你该不会 Java 程序员转来的吧? 容器区分的这么详细。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1006 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 20:21 · PVG 04:21 · LAX 13:21 · JFK 16:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.