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

False == (False in [False])

  •  
  •   glasslion · 2015-10-16 21:10:30 +08:00 · 3651 次点击
    这是一个创建于 3116 天前的主题,其中的信息可能已经有所发展或是发生改变。

    大家觉得这个表达式的值是什么?

    14 条回复    2015-10-19 23:51:54 +08:00
    latteczy
        1
    latteczy  
       2015-10-16 21:14:36 +08:00
    False
    hahastudio
        2
    hahastudio  
       2015-10-16 21:26:04 +08:00
    False in [False] => True
    False == True => False
    你要是挑个感觉会歧义的也行啊
    aaronlam
        3
    aaronlam  
       2015-10-16 21:52:16 +08:00
    楼上正解
    理解好运算优先级的话,这不是很容易得到答案吗?
    imn1
        4
    imn1  
       2015-10-16 22:26:22 +08:00
    True == (False in [False],)
    这个呢?
    kendetrics
        5
    kendetrics  
       2015-10-16 22:33:06 +08:00
    @imn1 False != [False] => False not in [False], ,对吧?
    Hello1995
        6
    Hello1995  
       2015-10-16 22:55:46 +08:00
    简单粗暴。过程正如 #2 所言。
    hahastudio
        7
    hahastudio  
       2015-10-16 23:28:52 +08:00
    @imn1
    True == tuple => False
    你这也就骗骗连语法都不懂的= =
    imn1
        8
    imn1  
       2015-10-16 23:33:57 +08:00
    @hahastudio
    不是问你,问 LZ 的
    gccplus
        9
    gccplus  
       2015-10-17 00:24:17 +08:00
    True == (False in [False])
    True

    True == (False in [False],)
    False

    一个逗号就不一样拉~~
    fourstring
        10
    fourstring  
       2015-10-17 15:48:14 +08:00
    @hahastudio 就是说,在这种情境下, Python 不会像 if object 这样调用对象的__bool__()方法来返回一个 bool 值进行计算?
    hahastudio
        11
    hahastudio  
       2015-10-17 16:58:28 +08:00
    @fourstring 你遇到的不是一个 object ,是一个表达式啊
    siteshen
        12
    siteshen  
       2015-10-17 18:55:16 +08:00
    @fourstring 另如果所有 a == b 都转换成 bool 类型再比较的话,那么 a == True 或者 a == False 就必然为真了。不是一个物种,你让 python 怎么自动转换。

    () == [] => False
    1 == 1.0 => True
    True == 1 => True
    False == 0 => False
    complex(1,0) == True
    fourstring
        13
    fourstring  
       2015-10-17 22:00:58 +08:00
    @siteshen
    @hahastudio
    感谢,一时没想明白……
    easyhappy
        14
    easyhappy  
       2015-10-19 23:51:54 +08:00
    False == False in [False]
    >>>True

    为什么是 True ?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1113 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 23:24 · PVG 07:24 · LAX 16:24 · JFK 19:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.