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

Python3 中的比较问题

  •  
  •   skai0dev · 2018-08-29 11:00:39 +08:00 · 2165 次点击
    这是一个创建于 2060 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近看到 Python 官方文档时,在标准库中的内置类型的比较部分卡住了,有一段感觉不是很明白,摘抄如下:

    Objects of different types, except different numeric types, never compare equal. Furthermore, some types (for example, function objects) support only a degenerate notion of comparison where any two objects of that type are unequal. The <, <=, > and >= operators will raise a TypeError exception when comparing a complex number with another built-in numeric type, when the objects are of different types that cannot be compared, or in other cases where there is no defined ordering

    原文在 https://docs.python.org/3/library/stdtypes.html#comparisons 那个表格的下面那一段,主要是那一句:Furthermore, some types (for example, function objects) support only a degenerate notion of comparison where any two objects of that type are unequal.
    这个degenerate notion of comparison是什么?字面理解的话好像是退化的比较概念,或者说是简化版的比较,这个指的是什么?

    已知的是:Python3 中不同类型的比较好像只有不同的数字类型能比较,而且复数也不能和其他的数字类型比较,其他的不同类型之间好像只能用"==","!=","is","is not"比较。

    Python2 中不同类型好像根据 id 来比较的,参考: Stack Overflow

    可能有点钻牛角尖了,请各位大神能指点一二,谢谢了。

    7 条回复    2018-08-31 10:16:53 +08:00
    frostming
        1
    frostming  
       2018-08-29 11:20:07 +08:00   ❤️ 1
    Furthermore, some types (for example, function objects) support only a degenerate notion of comparison where any two objects of that type are unequal.

    对于某些类型只支持部分的比较,即任意两个该类型的对象都是不等的。就是说,只支持==和!=,并且是比较其 id

    后面那段话意思是<, <=, >, >=的比较符在下列情况会抛出 TypeError
    1. 比较一个复数类型和其他类型的数字
    2. 对象属于不能比较的不同类型
    3. 对象的类型没有定义比较的方法(__lt__, __le__, __gt__, __ge__)
    geelaw
        2
    geelaw  
       2018-08-29 11:21:53 +08:00 via iPhone   ❤️ 1
    a degenerate notion of comparison where any two objects of that type are unequal

    ... where any two objects of that type are unequal

    就是只有比较是否是同一个对象的功能,没有比较不同对象是否语义上相等,也就是 object.ReferenceEquals
    skai0dev
        3
    skai0dev  
    OP
       2018-08-29 12:01:09 +08:00
    @frostming @geelaw 谢谢
    cyrbuzz
        4
    cyrbuzz  
       2018-08-29 12:44:27 +08:00
    嘿~,不知道你是用的什么字典,查单词不翻墙我一般用 柯林斯
    https://www.collinsdictionary.com/zh/dictionary/english/degenerate
    这是柯林斯查的 degenerate 的意思,第二条中说 degenerate 可以来形容低标准行为的人,同义词中有个 base,应该可以理解为"基础"。所以:
    degenerate notion of comparison
    应该可以理解为:
    基础(基本)的比较。
    希望有所帮助。
    skai0dev
        5
    skai0dev  
    OP
       2018-08-29 14:47:18 +08:00
    @cyrbuzz 我看文档不懂的时候一般使用 Google 翻译的插件,一般都能了解个大概,没怎么查过具体的某个单词,所以没怎么用过词典,不过谢谢你的推荐,收藏了
    Windsooon
        6
    Windsooon  
       2018-08-30 08:39:58 +08:00   ❤️ 1
    这是个好问题:

    def func1(): pass

    def func2(): pass

    # You can do this
    assert func1 != func2

    # But not this
    func1 > func2
    # TypeError: '>' not supported between instances of 'function' and 'function'

    "Degenerate" as in "not fully featured"; it mentions that you can check that functions are unequal, but you can't check whether one function is "greater than" another one. assert was only in my example to show that func1 != func2 is in fact True; you could leave out the assert and you'd just lose the result of the comparison — Zachary Ware
    Windsooon
        7
    Windsooon  
       2018-08-31 10:16:53 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   863 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 21:56 · PVG 05:56 · LAX 14:56 · JFK 17:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.