__gt__() 和方法 。python.org/3.9/reference/datamodel.html#object.__ge__" rel="external nofoll。Python中dataclass库( 二 )。" />

Python中dataclass库( 二 )

__le__()、python.org/3.9/reference/datamodel.html#object.__gt__" rel="external nofollow noreferrer">__gt__()和方法 。python.org/3.9/reference/datamodel.html#object.__ge__" rel="external nofollow noreferrer">__ge__()这些按顺序比较类,就好像它是其字段的元组一样 。比较中的两个实例必须是相同的类型 。如果order为真且eq为假,python.org/3.9/library/exceptions.html#ValueError" rel="external nofollow noreferrer">ValueError则引发 a 。
如果该类已经定义了python.org/3.9/reference/datamodel.html#object.__lt__" rel="external nofollow noreferrer">__lt__()、 python.org/3.9/reference/datamodel.html#object.__le__" rel="external nofollow noreferrer">__le__()、python.org/3.9/reference/datamodel.html#object.__gt__" rel="external nofollow noreferrer">__gt__()或中的任何一个,python.org/3.9/reference/datamodel.html#object.__ge__" rel="external nofollow noreferrer">__ge__()则 python.org/3.9/library/exceptions.html#TypeError" rel="external nofollow noreferrer">TypeError引发 。

  • unsafe_hash:if False(默认),python.org/3.9/reference/datamodel.html#object.__hash__" rel="external nofollow noreferrer">__hash__()根据how eqand frozenare set生成一个方法 。
    python.org/3.9/reference/datamodel.html#object.__hash__" rel="external nofollow noreferrer">__hash__()由 built-in 使用python.org/3.9/library/functions.html#hash" rel="external nofollow noreferrer">hash(),并且在将对象添加到散列集合(例如字典和集合)时使用 。拥有 a python.org/3.9/reference/datamodel.html#object.__hash__" rel="external nofollow noreferrer">__hash__()意味着类的实例是不可变的 。可变性是一个复杂的属性,它取决于程序员的意图、 的存在和行为,以及装饰器中的和标志python.org/3.9/reference/datamodel.html#object.__eq__" rel="external nofollow noreferrer">__eq__()的值 。eq``frozenpython.org/3.9/library/dataclasses.html#dataclasses.dataclass" rel="external nofollow noreferrer">dataclass()
    默认情况下,除非这样做是安全的,否则python.org/3.9/library/dataclasses.html#dataclasses.dataclass" rel="external nofollow noreferrer">dataclass()不会隐式添加方法 。python.org/3.9/reference/datamodel.html#object.__hash__" rel="external nofollow noreferrer">__hash__()它也不会添加或更改现有的明确定义的python.org/3.9/reference/datamodel.html#object.__hash__" rel="external nofollow noreferrer">__hash__()方法 。如文档中所述,设置类属性对 Python 具有特定含义 。__hash__ = Nonepython.org/3.9/reference/datamodel.html#object.__hash__" rel="external nofollow noreferrer">__hash__()
    如果python.org/3.9/reference/datamodel.html#object.__hash__" rel="external nofollow noreferrer">__hash__()没有显式定义,或者如果设置为None,则

    经验总结扩展阅读