V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
morefreeze
V2EX  ›  Python

Scrapy 的 Middleware 为什么要用字典的形式设置顺序而不直接用一个列表?

  •  
  •   morefreeze · Aug 20, 2019 · 2323 views
    This topic created in 2447 days ago, the information mentioned may be changed or developed.

    从新增的角度,列表也可以方便地在中间插入一个元素,删除也同理

    唯一的优势是可以用 None 来表示不使用,而列表虽然可以直接删掉,但容易让人不知道原来还存在过这个元素

    1 replies    2019-08-21 08:58:54 +08:00
    lshu
        1
    lshu  
       Aug 21, 2019
    其实主要的原因是一个 settings merge 的问题。
    假设 scrapy 默认中间件 settings 字典为 {'a':1,'b':10,c:'13'} ,而你自定义了 中间件 此时为 {'d':8,'e':12,'a':None} 这样能够很好地合并之后 {'a':None,'d':8,'b':10,'e':12,c:'13'} , 并且合并之后有序。 反之 如果是 列表 则做不到这种功能。 除非你这样写 [('a':1),('b':10)] 哈哈哈。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2687 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 14:46 · PVG 22:46 · LAX 07:46 · JFK 10:46
    ♥ Do have faith in what you're doing.