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

Python 3.6 要让所有字典有序?

  •  
  •   guyskk ·
    guyskk · 2016-10-03 13:35:57 +08:00 via Android · 5546 次点击
    这是一个创建于 2979 天前的主题,其中的信息可能已经有所发展或是发生改变。

    https://www.reddit.com/r/Python/comments/55iqpo/guido_on_dicts_in_python_36/

    https://mail.python.org/pipermail/python-dev/2016-September/146348.html

    I've been asked about this. Here's my opinion on the letter of the law in 3.6:

    • keyword args are ordered
    • the namespace passed to a metaclass is ordered by definition order
    • ditto for the class dict

    A compliant implementation may ensure the above three requirements either by making all dicts ordered, or by providing a custom dict subclass (e.g. OrderedDict) in those three cases.

    I'd like to handwave on the ordering of all other dicts. Yes, in CPython 3.6 and in PyPy they are all ordered, but it's an implementation detail. I don't want to force all other implementations to follow suit. I also don't want too many people start depending on this, since their code will break in 3.5. (Code that needs to depend on the ordering of keyword args or class attributes should be relatively uncommon; but people will start to depend on the ordering of all dicts all too easily. I want to remind them that they are taking a risk, and their code won't be backwards compatible.)

    --Guido

    16 条回复    2016-10-04 22:14:23 +08:00
    terence4444
        1
    terence4444  
       2016-10-03 13:40:26 +08:00 via iPhone
    记得有一个 OrderedDict 可以用…
    Patrick95
        2
    Patrick95  
       2016-10-03 13:44:19 +08:00 via iPhone
    我觉得有序挺好的,无序也没什么影响。
    tabris17
        3
    tabris17  
       2016-10-03 13:54:15 +08:00
    早就该这么做了
    guyskk
        4
    guyskk  
    OP
       2016-10-03 13:56:12 +08:00 via Android
    @Patrick95 感觉这样会导致性能下降,这种字典结构在大多数语言里面都是无序的
    tairan2006
        5
    tairan2006  
       2016-10-03 14:02:01 +08:00 via Android
    默认实现由哈希表转为红黑树(或跳表)吧,性能当然有影响
    kkzxak47
        6
    kkzxak47  
       2016-10-03 14:08:13 +08:00 via Android   ❤️ 1
    @guyskk 我记得有序是一个实现方式改变带来的副产品,不是刻意追求
    kkzxak47
        7
    kkzxak47  
       2016-10-03 14:11:04 +08:00 via Android
    guyskk
        8
    guyskk  
    OP
       2016-10-03 14:51:27 +08:00 via Android
    doubleflower
        9
    doubleflower  
       2016-10-03 15:42:14 +08:00
    这功能挺人性化的,连打印调试都更好读了,而且性能损失很小可以忽略。
    skydiver
        10
    skydiver  
       2016-10-03 15:44:04 +08:00 via Android
    终于和最好的语言 PHP 看齐了
    awanabe
        11
    awanabe  
       2016-10-03 17:22:07 +08:00
    @terence4444 OrderedDict 里面是维护了一个链表, 如果数据量大,相当于 size 是两倍... 还是挺吓人的🎺
    cortexm3
        12
    cortexm3  
       2016-10-03 20:10:58 +08:00
    @guyskk +1
    zonghua
        13
    zonghua  
       2016-10-03 22:29:35 +08:00
    性能再一次降低了
    tabris17
        14
    tabris17  
       2016-10-03 22:31:46 +08:00
    @tairan2006 怎么可能。无非是哈希表的 bucket 加上链表而已,多维护一个链表罢了,对性能影响微乎其微
    jiang42
        15
    jiang42  
       2016-10-03 22:33:56 +08:00 via iPhone
    @zonghua 我的理解,性能不会降低,内存占用减少

    具体参见 @kkzxak47 贴出的邮件

    Python 其实一直致力于性能的提升, e.g. os.scandir
    ruoyu0088
        16
    ruoyu0088  
       2016-10-04 22:14:23 +08:00   ❤️ 1
    这里有性能比较: https://gist.github.com/methane/e7a2ae389ca13905508905f5fa4ad46c
    内存可以节省 20%左右。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1235 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 18:16 · PVG 02:16 · LAX 10:16 · JFK 13:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.