summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2003-02-07 22:50:28 (GMT)
committerTim Peters <tim.peters@gmail.com>2003-02-07 22:50:28 (GMT)
commit07534a607bd3d2fd232323811a83279acda10885 (patch)
tree129fc63de44a72f412e64c591746530324261500 /Doc
parent275666fd50eaf494448ba102652d4273b135a017 (diff)
downloadcpython-07534a607bd3d2fd232323811a83279acda10885.zip
cpython-07534a607bd3d2fd232323811a83279acda10885.tar.gz
cpython-07534a607bd3d2fd232323811a83279acda10885.tar.bz2
Comparison for timedelta, time, date and datetime objects: __eq__ and
__ne__ no longer complain if they don't know how to compare to the other thing. If no meaningful way to compare is known, saying "not equal" is sensible. This allows things like if adatetime in some_sequence: and somedict[adatetime] = whatever to work as expected even if some_sequence contains non-datetime objects, or somedict non-datetime keys, because they only call __eq__. It still complains (raises TypeError) for mixed-type comparisons in contexts that require a total ordering, such as list.sort(), use as a key in a BTree-based data structure, and cmp().
Diffstat (limited to 'Doc')
-rw-r--r--Doc/lib/libdatetime.tex20
1 files changed, 19 insertions, 1 deletions
diff --git a/Doc/lib/libdatetime.tex b/Doc/lib/libdatetime.tex
index f49fc49..4d64499 100644
--- a/Doc/lib/libdatetime.tex
+++ b/Doc/lib/libdatetime.tex
@@ -257,6 +257,11 @@ support certain additions and subtractions with \class{date} and
Comparisons of \class{timedelta} objects are supported with the
\class{timedelta} object representing the smaller duration considered
to be the smaller timedelta.
+In order to stop mixed-type comparisons from falling back to the
+default comparison by object address, when a \class{timedelta} object is
+compared to an object of a different type, \exception{TypeError} is
+raised unless the comparison is \code{==} or \code{!=}. The latter
+cases return \constant{False} or \constant{True}, respectively.
\class{timedelta} objects are hashable (usable as dictionary keys),
support efficient pickling, and in Boolean contexts, a \class{timedelta}
@@ -402,6 +407,10 @@ isn't also a \class{date} object. However, \code{NotImplemented}
is returned instead if the other comparand has a
\method{timetuple} attribute. This hook gives other kinds of
date objects a chance at implementing mixed-type comparison.
+If not, when a \class{date} object is
+compared to an object of a different type, \exception{TypeError} is
+raised unless the comparison is \code{==} or \code{!=}. The latter
+cases return \constant{False} or \constant{True}, respectively.
\end{description}
@@ -743,7 +752,11 @@ the other is aware, \exception{TypeError} is raised. If both
\code{NotImplemented} is returned instead if the other comparand
has a \method{timetuple} attribute. This hook gives other
kinds of date objects a chance at implementing mixed-type
- comparison.}
+ comparison. If not, when a \class{datetime} object is
+ compared to an object of a different type, \exception{TypeError}
+ is raised unless the comparison is \code{==} or \code{!=}. The
+ latter cases return \constant{False} or \constant{True},
+ respectively.}
\end{description}
@@ -1023,6 +1036,11 @@ Supported operations:
comparands are aware and have different \member{tzinfo} members,
the comparands are first adjusted by subtracting their UTC offsets
(obtained from \code{self.utcoffset()}).
+ In order to stop mixed-type comparisons from falling back to the
+ default comparison by object address, when a \class{time} object is
+ compared to an object of a different type, \exception{TypeError} is
+ raised unless the comparison is \code{==} or \code{!=}. The latter
+ cases return \constant{False} or \constant{True}, respectively.
\item
hash, use as dict key