Copyright (C) 2000-2012 |
GNU Info (python2.1-ref.info)ComparisonsComparisons =========== Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation. Also unlike C, expressions like `a < b < c' have the interpretation that is conventional in mathematics: comparison: or_expr (comp_operator or_expr)* comp_operator: "<"|">"|"=="|">="|"<="|"<>"|"!="|"is" ["not"]|["not"] "in" Comparisons yield integer values: `1' for true, `0' for false. Comparisons can be chained arbitrarily, e.g., `x < y <= z' is equivalent to `x < y and y <= z', except that `y' is evaluated only once (but in both cases `z' is not evaluated at all when `x < y' is found to be false). Formally, if A, B, C, ..., Y, Z are expressions and OPA, OPB, ..., OPY are comparison operators, then A OPA B OPB C ...Y OPY Z is equivalent to A OPA B `and' B OPB C `and' ... Y OPY Z, except that each expression is evaluated at most once. Note that A OPA B OPB C doesn't imply any kind of comparison between A and C, so that, e.g., `x < y > z' is perfectly legal (though perhaps not pretty). The forms `<>' and `!=' are equivalent; for consistency with C, `!=' is preferred; where `!=' is mentioned below `<>' is also accepted. The `<>' spelling is considered obsolescent. The operators `<', `>', `==', `>=', `<=', and `!=' compare the values of two objects. The objects need not have the same type. If both are numbers, they are coverted to a common type. Otherwise, objects of different types _always_ compare unequal, and are ordered consistently but arbitrarily. (This unusual definition of comparison was used to simplify the definition of operations like sorting and the `in' and `not in' operators. In the future, the comparison rules for objects of different types are likely to change.) Comparison of objects of the same type depends on the type: * Numbers are compared arithmetically. * Strings are compared lexicographically using the numeric equivalents (the result of the built-in function `ord()') of their characters. Unicode and 8-bit strings are fully interoperable in this behavior. * Tuples and lists are compared lexicographically using comparison of corresponding items. * Mappings (dictionaries) are compared through lexicographic comparison of their sorted (key, value) lists.(1) * Most other types compare unequal unless they are the same object; the choice whether one object is considered smaller or larger than another one is made arbitrarily but consistently within one execution of a program. The operators `in' and `not in' test for set membership. `X in S' evaluates to true if X is a member of the set S, and false otherwise. `X not in S' returns the negation of `X in S'. The set membership test has traditionally been bound to sequences; an object is a member of a set if the set is a sequence and contains an element equal to that object. However, it is possible for an object to support membership tests without being a sequence. For the list and tuple types, `X in Y' is true if and only if there exists an index I such that `X == Y[I]' is true. For the Unicode and string types, `X in Y' is true if and only if there exists an index I such that `X == Y[I]' is true. If `X' is not a string or Unicode object of length `1', a `TypeError' exception is raised. For user-defined classes which define the `__contains__()' method, `X in Y' is true if and only if `Y.__contains__(X)' is true. For user-defined classes which do not define `__contains__()' and do define `__getitem__()', `X in Y' is true if and only if there is a non-negative integer index I such that `X == Y[I]', and all lower integer indices do not raise `IndexError' exception. (If any other exception is raised, it is as if `in' raised that exception). The operator `not in' is defined to have the inverse true value of `in'. The operators `is' and `is not' test for object identity: `X is Y' is true if and only if X and Y are the same object. `X is not Y' yields the inverse truth value. ---------- Footnotes ---------- (1) This is expensive since it requires sorting the keys first, but it is about the only sensible definition. An earlier version of Python compared dictionaries by identity only, but this caused surprises because people expected to be able to test a dictionary for emptiness by comparing it to `{}'. automatically generated by info2www version 1.2.2.9 |