Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(158476)

Side by Side Diff: Doc/reference/expressions.rst

Issue 12067: Doc: remove errors about mixed-type comparisons.
Patch Set: Created 7 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 .. _expressions: 2 .. _expressions:
3 3
4 *********** 4 ***********
5 Expressions 5 Expressions
6 *********** 6 ***********
7 7
8 .. index:: expression, BNF 8 .. index:: expression, BNF
9 9
10 This chapter explains the meaning of the elements of expressions in Python. 10 This chapter explains the meaning of the elements of expressions in Python.
(...skipping 1023 matching lines...) Expand 10 before | Expand all | Expand 10 after
1034 1034
1035 Formally, if *a*, *b*, *c*, ..., *y*, *z* are expressions and *op1*, *op2*, ..., 1035 Formally, if *a*, *b*, *c*, ..., *y*, *z* are expressions and *op1*, *op2*, ...,
1036 *opN* are comparison operators, then ``a op1 b op2 c ... y opN z`` is equivalent 1036 *opN* are comparison operators, then ``a op1 b op2 c ... y opN z`` is equivalent
1037 to ``a op1 b and b op2 c and ... y opN z``, except that each expression is 1037 to ``a op1 b and b op2 c and ... y opN z``, except that each expression is
1038 evaluated at most once. 1038 evaluated at most once.
1039 1039
1040 Note that ``a op1 b op2 c`` doesn't imply any kind of comparison between *a* and 1040 Note that ``a op1 b op2 c`` doesn't imply any kind of comparison between *a* and
1041 *c*, so that, e.g., ``x < y > z`` is perfectly legal (though perhaps not 1041 *c*, so that, e.g., ``x < y > z`` is perfectly legal (though perhaps not
1042 pretty). 1042 pretty).
1043 1043
1044 The operators ``<``, ``>``, ``==``, ``>=``, ``<=``, and ``!=`` compare the 1044 The operators ``<``, ``>``, ``==``, ``>=``, ``<=``, and ``!=`` compare the
1045 values of two objects. The objects need not have the same type. If both are 1045 values of two objects. The objects need not be of the same type. With
1046 numbers, they are converted to a common type. Otherwise, the ``==`` and ``!=`` 1046 ``==`` and ``!=``, an object is equal to itself and different objects
1047 operators *always* consider objects of different types to be unequal, while the 1047 (a is not b) compare unequal, unless the class of the first define a
1048 ``<``, ``>``, ``>=`` and ``<=`` operators raise a :exc:`TypeError` when 1048 specific :meth:`__eq__()` and :meth:`__ne__()`. With ``<``, ``>``,
ezio.melotti 2013/02/23 07:51:56 These should be :meth:`~object.__eq__` (or whateve
mikehoy 2013/03/02 23:12:47 Currently working on this.
1049 comparing objects of different types that do not implement these operators for 1049 ``<=``, and ``>=``, comparison raises a :exc:`TypeError`, unless the class
1050 the given pair of types. You can control comparison behavior of objects of 1050 of the first object defines specific :meth:`__lt__()`, :meth:`__gt__()`,
1051 non-built-in types by defining rich comparison methods like :meth:`__gt__`, 1051 :meth:`__le__()`, and :meth:`__ge__()`, or the class of the second defines
1052 described in section :ref:`customization`. 1052 the reflected method (:meth:`__ge__()` reflects :meth:`__lt__()`, etcetera.
1053 1053 Built-in numbers compare as expected, even when of different types.
1054 Comparison of objects of the same type depends on the type: 1054 Built-in sequences compare lexicographically.
1055
1056 * Numbers are compared arithmetically.
1057
1058 * The values :const:`float('NaN')` and :const:`Decimal('NaN')` are special.
1059 The are identical to themselves, ``x is x`` but are not equal to themselves,
1060 ``x != x``. Additionally, comparing any value to a not-a-number value
1061 will return ``False``. For example, both ``3 < float('NaN')`` and
1062 ``float('NaN') < 3`` will return ``False``.
1063
1064 * Bytes objects are compared lexicographically using the numeric values of their
1065 elements.
1066
1067 * Strings are compared lexicographically using the numeric equivalents (the
1068 result of the built-in function :func:`ord`) of their characters. [#]_ String
1069 and bytes object can't be compared!
1070
1071 * Tuples and lists are compared lexicographically using comparison of
1072 corresponding elements. This means that to compare equal, each element must
1073 compare equal and the two sequences must be of the same type and have the same
1074 length.
1075
1076 If not equal, the sequences are ordered the same as their first differing
1077 elements. For example, ``[1,2,x] <= [1,2,y]`` has the same value as
1078 ``x <= y``. If the corresponding element does not exist, the shorter
1079 sequence is ordered first (for example, ``[1,2] < [1,2,3]``).
1080
1081 * Mappings (dictionaries) compare equal if and only if they have the same
1082 ``(key, value)`` pairs. Order comparisons ``('<', '<=', '>=', '>')``
1083 raise :exc:`TypeError`.
1084
1085 * Sets and frozensets define comparison operators to mean subset and superset
1086 tests. Those relations do not define total orderings (the two sets ``{1,2}``
1087 and {2,3} are not equal, nor subsets of one another, nor supersets of one
1088 another). Accordingly, sets are not appropriate arguments for functions
1089 which depend on total ordering. For example, :func:`min`, :func:`max`, and
1090 :func:`sorted` produce undefined results given a list of sets as inputs.
1091
1092 * Most other objects of built-in types compare unequal unless they are the same
1093 object; the choice whether one object is considered smaller or larger than
1094 another one is made arbitrarily but consistently within one execution of a
1095 program.
1096 1055
1097 Comparison of objects of the differing types depends on whether either 1056 Comparison of objects of the differing types depends on whether either
1098 of the types provide explicit support for the comparison. Most numeric types 1057 of the types provide explicit support for the comparison. Most numeric types
1099 can be compared with one another, but comparisons of :class:`float` and 1058 can be compared with one another, but comparisons of :class:`float` and
1100 :class:`Decimal` are not supported to avoid the inevitable confusion arising 1059 :class:`Decimal` are not supported to avoid the inevitable confusion arising
1101 from representation issues such as ``float('1.1')`` being inexactly represented 1060 from representation issues such as ``float('1.1')`` being inexactly represented
1102 and therefore not exactly equal to ``Decimal('1.1')`` which is. When 1061 and therefore not exactly equal to ``Decimal('1.1')`` which is. When
1103 cross-type comparison is not supported, the comparison method returns 1062 cross-type comparison is not supported, the comparison method returns
1104 ``NotImplemented``. This can create the illusion of non-transitivity between 1063 ``NotImplemented``. This can create the illusion of non-transitivity between
1105 supported cross-type comparisons and unsupported comparisons. For example, 1064 supported cross-type comparisons and unsupported comparisons. For example,
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
1382 .. [#] Due to automatic garbage-collection, free lists, and the dynamic nature o f 1341 .. [#] Due to automatic garbage-collection, free lists, and the dynamic nature o f
1383 descriptors, you may notice seemingly unusual behaviour in certain uses of 1342 descriptors, you may notice seemingly unusual behaviour in certain uses of
1384 the :keyword:`is` operator, like those involving comparisons between instance 1343 the :keyword:`is` operator, like those involving comparisons between instance
1385 methods, or constants. Check their documentation for more info. 1344 methods, or constants. Check their documentation for more info.
1386 1345
1387 .. [#] The ``%`` operator is also used for string formatting; the same 1346 .. [#] The ``%`` operator is also used for string formatting; the same
1388 precedence applies. 1347 precedence applies.
1389 1348
1390 .. [#] The power operator ``**`` binds less tightly than an arithmetic or 1349 .. [#] The power operator ``**`` binds less tightly than an arithmetic or
1391 bitwise unary operator on its right, that is, ``2**-1`` is ``0.5``. 1350 bitwise unary operator on its right, that is, ``2**-1`` is ``0.5``.
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+