推荐学习书目
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
wuwukai007
V2EX  ›  Python

python2.7 None < 5 return True, python3 就直接报错了

  •  
  •   wuwukai007 · Jun 17, 2021 · 1915 views
    This topic created in 1815 days ago, the information mentioned may be changed or developed.
    x = {'q2':2}
    if x.get('q1',None) < 5:
      # todo
    

    字典取值 默认值 None,代码竟然没报错,还到 todo 里面执行了!

    3 replies    2021-06-17 23:26:56 +08:00
    dreampuf
        1
    dreampuf  
       Jun 17, 2021
    先用 `variable is None` 判断。`dict.get` default 填 None 是坑。Python2 不同类型比较按照类型名称字符大小比较,'i'nt 总是小于 'S'tr 。其中 `None` 是最小的类型,比任何类型都小。

    https://stackoverflow.com/questions/8961005/comparing-none-with-built-in-types-using-arithmetic-operators
    LeeReamond
        2
    LeeReamond  
       Jun 17, 2021
    @dreampuf 感觉不是一个良好的设计,说不出什么逻辑联系,只能当做特例来记,也不起到多少方便的作用
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1016 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 18:45 · PVG 02:45 · LAX 11:45 · JFK 14:45
    ♥ Do have faith in what you're doing.