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().
This commit is contained in:
Tim Peters 2003-02-07 22:50:28 +00:00
parent 275666fd50
commit 07534a607b
4 changed files with 155 additions and 58 deletions

View file

@ -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