78 Chapter 6. Expressions The Python Language Reference, Release 3.9.5 Lexicographical comparison between built-in collections works as follows: Order comparisons ($_{<}$ ,$_{>}$ ,$_{<=}$ , and$_{>=}$ ) raise$_{TypeError}$. They define order comparison operators to mean subset and superset tests. Those relations do not define total orderings (for example, the two sets {1,2} and {2,3} are not equal, nor subsets of one another, nor supersets of one another). Accordingly, sets are not appropriate arguments for functions which depend on total ordering (for example, min() , max() , and sorted() produce undefined results given a list of sets as inputs). Comparison of sets enforces reflexivity of its elements. The last two expressions apply to totally ordered collections (e.g. to sequences, but not to sets or mappings). See also the$_{total_ordering()}$ decorator. Python does not enforce these consistency rules. In fact, the not-a-number values are an example for not following these rules. • The$_{hash()}$result should be consistent with equality. Objects that are equal should either have the same hash value, or be marked as unhashable. • Comparison should be transitive. The following (non-exhaustive) examples illustrate that: • Inverse comparison should result in the boolean negation. In other words, the following expressions should have the same result: • Comparison should be symmetric. In other words, the following expressions should have the same result: • Equality comparison should be reflexive. In other words, identical objects should compare equal: • Most other built-in types have no comparison methods implemented, so they inherit the default comparison behavior. User-defined classes that customize their comparison behavior should follow some consistency rules, if possible: • Sets (instances of$_{set}$ or$_{frozenset}$) can be compared within and across their types. • Mappings (instances of dict ) compare equal if and only if they have equal (key, value) pairs. Equality comparison of the keys and values enforces reflexivity. – Collections that support order comparison are ordered the same as their first unequal elements (for ex- ample, [1,2,x] <= [1,2,y] has the same value as x <= y ). If a corresponding element does not exist, the shorter collection is ordered first (for example, [1,2] < [1,2,3] is true). – For two collections to compare equal, they must be of the same type, have the same length, and each pair of corresponding elements must compare equal (for example,$_{[1,2] == (1,2)}$ is false because the type is not the same). $_{x is y}$implies$_{x == y}$ x == y and$_{y == x}$ x != y and$_{y != x}$ $_{x < y}$and$_{y > x}$ x <= y and$_{y >= x}$ x > y and y > z implies$_{x > z}$ x < y and y <= z implies$_{x < z}$ x == y and$_{not x != y}$ $_{x < y}$and$_{not x >= y}$ (for total ordering) $_{x > y}$and$_{not x <= y}$ (for total ordering)