@@ -400,30 +400,7 @@ objects (see below).
400
400
the :func: `divmod ` function. True division and multiplication of a
401
401
:class: `timedelta ` object by a :class: `float ` object are now supported.
402
402
403
-
404
- Comparisons of :class: `timedelta ` objects are supported, with some caveats.
405
-
406
- The comparisons ``== `` or ``!= `` *always * return a :class: `bool `, no matter
407
- the type of the compared object::
408
-
409
- >>> from datetime import timedelta
410
- >>> delta1 = timedelta(seconds=57)
411
- >>> delta2 = timedelta(hours=25, seconds=2)
412
- >>> delta2 != delta1
413
- True
414
- >>> delta2 == 5
415
- False
416
-
417
- For all other comparisons (such as ``< `` and ``> ``), when a :class: `timedelta `
418
- object is compared to an object of a different type, :exc: `TypeError `
419
- is raised::
420
-
421
- >>> delta2 > delta1
422
- True
423
- >>> delta2 > 5
424
- Traceback (most recent call last):
425
- File "<stdin>", line 1, in <module>
426
- TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
403
+ :class: `timedelta ` objects support equality and order comparisons.
427
404
428
405
In Boolean contexts, a :class: `timedelta ` object is
429
406
considered to be true if and only if it isn't equal to ``timedelta(0) ``.
@@ -614,8 +591,13 @@ Supported operations:
614
591
+-------------------------------+----------------------------------------------+
615
592
| ``timedelta = date1 - date2 `` | \( 3) |
616
593
+-------------------------------+----------------------------------------------+
617
- | ``date1 < date2 `` | *date1 * is considered less than *date2 * when |
618
- | | *date1 * precedes *date2 * in time. (4) |
594
+ | | ``date1 == date2 `` | Equality comparison. (4) |
595
+ | | ``date1 != date2 `` | |
596
+ +-------------------------------+----------------------------------------------+
597
+ | | ``date1 < date2 `` | Order comparison. (5) |
598
+ | | ``date1 > date2 `` | |
599
+ | | ``date1 <= date2 `` | |
600
+ | | ``date1 >= date2 `` | |
619
601
+-------------------------------+----------------------------------------------+
620
602
621
603
Notes:
@@ -635,15 +617,12 @@ Notes:
635
617
timedelta.microseconds are 0, and date2 + timedelta == date1 after.
636
618
637
619
(4)
620
+ :class: `date ` objects are equal if they represent the same date.
621
+
622
+ (5)
623
+ *date1 * is considered less than *date2 * when *date1 * precedes *date2 * in time.
638
624
In other words, ``date1 < date2 `` if and only if ``date1.toordinal() <
639
- date2.toordinal() ``. Date comparison raises :exc: `TypeError ` if
640
- the other comparand isn't also a :class: `date ` object. However,
641
- ``NotImplemented `` is returned instead if the other comparand has a
642
- :attr: `~date.timetuple ` attribute. This hook gives other kinds of date objects a
643
- chance at implementing mixed-type comparison. If not, when a :class: `date `
644
- object is compared to an object of a different type, :exc: `TypeError ` is raised
645
- unless the comparison is ``== `` or ``!= ``. The latter cases return
646
- :const: `False ` or :const: `True `, respectively.
625
+ date2.toordinal() ``.
647
626
648
627
In Boolean contexts, all :class: `date ` objects are considered to be true.
649
628
@@ -1167,8 +1146,13 @@ Supported operations:
1167
1146
+---------------------------------------+--------------------------------+
1168
1147
| ``timedelta = datetime1 - datetime2 `` | \( 3) |
1169
1148
+---------------------------------------+--------------------------------+
1170
- | ``datetime1 < datetime2 `` | Compares :class: `.datetime ` to |
1171
- | | :class: `.datetime `. (4) |
1149
+ | | ``datetime1 == datetime2 `` | Equality comparison. (4) |
1150
+ | | ``datetime1 != datetime2 `` | |
1151
+ +---------------------------------------+--------------------------------+
1152
+ | | ``datetime1 < datetime2 `` | Order comparison. (5) |
1153
+ | | ``datetime1 > datetime2 `` | |
1154
+ | | ``datetime1 <= datetime2 `` | |
1155
+ | | ``datetime1 >= datetime2 `` | |
1172
1156
+---------------------------------------+--------------------------------+
1173
1157
1174
1158
(1)
@@ -1196,40 +1180,41 @@ Supported operations:
1196
1180
are done in this case.
1197
1181
1198
1182
If both are aware and have different :attr: `~.datetime.tzinfo ` attributes, ``a-b `` acts
1199
- as if *a * and *b * were first converted to naive UTC datetimes first . The
1183
+ as if *a * and *b * were first converted to naive UTC datetimes. The
1200
1184
result is ``(a.replace(tzinfo=None) - a.utcoffset()) - (b.replace(tzinfo=None)
1201
1185
- b.utcoffset()) `` except that the implementation never overflows.
1202
1186
1203
1187
(4)
1188
+ :class: `.datetime ` objects are equal if they represent the same date
1189
+ and time, taking into account the time zone.
1190
+
1191
+ Naive and aware :class: `!datetime ` objects are never equal.
1192
+ :class: `!datetime ` objects are never equal to :class: `date ` objects
1193
+ that are not also :class: `!datetime ` instances, even if they represent
1194
+ the same date.
1195
+
1196
+ If both comparands are aware and have different :attr: `~.datetime.tzinfo `
1197
+ attributes, the comparison acts as comparands were first converted to UTC
1198
+ datetimes except that the implementation never overflows.
1199
+ :class: `!datetime ` instances in a repeated interval are never equal to
1200
+ :class: `!datetime ` instances in other time zone.
1201
+
1202
+ (5)
1204
1203
*datetime1 * is considered less than *datetime2 * when *datetime1 * precedes
1205
- *datetime2 * in time.
1204
+ *datetime2 * in time, taking into account the time zone .
1206
1205
1207
- If one comparand is naive and the other is aware, :exc: ` TypeError `
1208
- is raised if an order comparison is attempted. For equality
1209
- comparisons, naive instances are never equal to aware instances .
1206
+ Order comparison between naive and aware :class: ` .datetime ` objects,
1207
+ as well as a :class: ` !datetime ` object and a :class: ` !date ` object
1208
+ that is not also a :class: ` !datetime ` instance, raises :exc: ` TypeError ` .
1210
1209
1211
- If both comparands are aware, and have the same :attr: `~.datetime.tzinfo ` attribute, the
1212
- common :attr: `~.datetime.tzinfo ` attribute is ignored and the base datetimes are
1213
- compared. If both comparands are aware and have different :attr: `~.datetime.tzinfo `
1214
- attributes, the comparands are first adjusted by subtracting their UTC
1215
- offsets (obtained from ``self.utcoffset() ``).
1210
+ If both comparands are aware and have different :attr: `~.datetime.tzinfo `
1211
+ attributes, the comparison acts as comparands were first converted to UTC
1212
+ datetimes except that the implementation never overflows.
1216
1213
1217
1214
.. versionchanged :: 3.3
1218
1215
Equality comparisons between aware and naive :class: `.datetime `
1219
1216
instances don't raise :exc: `TypeError `.
1220
1217
1221
- .. note ::
1222
-
1223
- In order to stop comparison from falling back to the default scheme of comparing
1224
- object addresses, datetime comparison normally raises :exc: `TypeError ` if the
1225
- other comparand isn't also a :class: `.datetime ` object. However,
1226
- ``NotImplemented `` is returned instead if the other comparand has a
1227
- :attr: `~.datetime.timetuple ` attribute. This hook gives other kinds of date objects a
1228
- chance at implementing mixed-type comparison. If not, when a :class: `.datetime `
1229
- object is compared to an object of a different type, :exc: `TypeError ` is raised
1230
- unless the comparison is ``== `` or ``!= ``. The latter cases return
1231
- :const: `False ` or :const: `True `, respectively.
1232
-
1233
1218
Instance methods:
1234
1219
1235
1220
.. method :: datetime.date()
@@ -1760,21 +1745,18 @@ Instance attributes (read-only):
1760
1745
1761
1746
.. versionadded :: 3.6
1762
1747
1763
- :class: `.time ` objects support comparison of :class: `.time ` to :class: `.time `,
1764
- where *a * is considered less
1765
- than *b * when *a * precedes *b * in time. If one comparand is naive and the other
1766
- is aware, :exc: `TypeError ` is raised if an order comparison is attempted. For equality
1767
- comparisons, naive instances are never equal to aware instances.
1748
+ :class: `.time ` objects support equality and order comparisons,
1749
+ where *a * is considered less than *b * when *a * precedes *b * in time.
1750
+
1751
+ Naive and aware :class: `!time ` objects are never equal.
1752
+ Order comparison between naive and aware :class: `!time ` objects raises
1753
+ :exc: `TypeError `.
1768
1754
1769
1755
If both comparands are aware, and have
1770
1756
the same :attr: `~.time.tzinfo ` attribute, the common :attr: `!tzinfo ` attribute is
1771
1757
ignored and the base times are compared. If both comparands are aware and
1772
1758
have different :attr: `!tzinfo ` attributes, the comparands are first adjusted by
1773
- subtracting their UTC offsets (obtained from ``self.utcoffset() ``). In order
1774
- to stop mixed-type comparisons from falling back to the default comparison by
1775
- object address, when a :class: `.time ` object is compared to an object of a
1776
- different type, :exc: `TypeError ` is raised unless the comparison is ``== `` or
1777
- ``!= ``. The latter cases return :const: `False ` or :const: `True `, respectively.
1759
+ subtracting their UTC offsets (obtained from ``self.utcoffset() ``).
1778
1760
1779
1761
.. versionchanged :: 3.3
1780
1762
Equality comparisons between aware and naive :class: `.time ` instances
0 commit comments