为什么 Python 3.7 数据类不支持 < > <= 和 >=,或者它们支持吗?

Why don't Python 3.7 dataclasses support < > <= and >=, or do they?

对于Transcrypt Python to JavaScript compiler I am currently using the new @dataclass decorator. I had expected that ==, !=, <, >, >=, <= would be supported, as per the PEP's abstract的3.7.1版本,但好像不是这样的:

from dataclasses import dataclass

@dataclass
class C:
    x: int = 10

有些比较无效:

>>> c1 = C(1)
>>> c2 = C(2)
>>> c1 == c2  # ok
False
>>> c1 < c2  # crash
TypeError: '<' not supported between instances of 'C' and 'C'

除了 ==!= 之外,为什么不支持比较运算符?还是我忽略了什么?

他们有,只是不是默认的。每 PEP-557:

The parameters to dataclass are:

...

  • order: If true (the default is False), __lt__, __le__, __gt__, and __ge__ methods will be generated. These compare the class as if it were a tuple of its fields, in order. Both instances in the comparison must be of the identical type. If order is true and eq is false, a ValueError is raised.

所以你想要@dataclass(order=True).