Python3 中的比较问题

2018-08-29 11:00:39 +08:00
 skai0dev

最近看到 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

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

2168 次点击
所在节点    Python
7 条回复
frostming
2018-08-29 11:20:07 +08:00
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
2018-08-29 11:21:53 +08:00
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
2018-08-29 12:01:09 +08:00
@frostming @geelaw 谢谢
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
2018-08-29 14:47:18 +08:00
@cyrbuzz 我看文档不懂的时候一般使用 Google 翻译的插件,一般都能了解个大概,没怎么查过具体的某个单词,所以没怎么用过词典,不过谢谢你的推荐,收藏了
Windsooon
2018-08-30 08:39:58 +08:00
这是个好问题:

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
2018-08-31 10:16:53 +08:00

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/484186

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX