Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 05f5ab7

Browse files
committed
Remove references to __cmp__.
1 parent 23dbc6e commit 05f5ab7

5 files changed

Lines changed: 44 additions & 68 deletions

File tree

Doc/glossary.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ Glossary
246246
hashable
247247
An object is *hashable* if it has a hash value which never changes during
248248
its lifetime (it needs a :meth:`__hash__` method), and can be compared to
249-
other objects (it needs an :meth:`__eq__` or :meth:`__cmp__` method).
250-
Hashable objects which compare equal must have the same hash value.
249+
other objects (it needs an :meth:`__eq__` method). Hashable objects which
250+
compare equal must have the same hash value.
251251

252252
Hashability makes an object usable as a dictionary key and a set member,
253253
because these data structures use the hash value internally.

Doc/library/decimal.rst

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -375,15 +375,14 @@ Decimal objects
375375

376376
.. method:: compare(other[, context])
377377

378-
Compare the values of two Decimal instances. This operation behaves in
379-
the same way as the usual comparison method :meth:`__cmp__`, except that
380-
:meth:`compare` returns a Decimal instance rather than an integer, and if
381-
either operand is a NaN then the result is a NaN::
382-
383-
a or b is a NaN ==> Decimal('NaN')
384-
a < b ==> Decimal('-1')
385-
a == b ==> Decimal('0')
386-
a > b ==> Decimal('1')
378+
Compare the values of two Decimal instances. :meth:`compare` returns a
379+
Decimal instance, and if either operand is a NaN then the result is a
380+
NaN::
381+
382+
a or b is a NaN ==> Decimal('NaN')
383+
a < b ==> Decimal('-1')
384+
a == b ==> Decimal('0')
385+
a > b ==> Decimal('1')
387386

388387
.. method:: compare_signal(other[, context])
389388

Doc/library/stdtypes.rst

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,23 +173,21 @@ any operand is a complex number, the objects are of different types that cannot
173173
be compared, or other cases where there is no defined ordering.
174174

175175
.. index::
176-
single: __cmp__() (instance method)
177176
single: __eq__() (instance method)
178177
single: __ne__() (instance method)
179178
single: __lt__() (instance method)
180179
single: __le__() (instance method)
181180
single: __gt__() (instance method)
182181
single: __ge__() (instance method)
183182

184-
Instances of a class normally compare as non-equal unless the class defines the
185-
:meth:`__eq__` or :meth:`__cmp__` method.
183+
Non-identical instances of a class normally compare as non-equal unless the
184+
class defines the :meth:`__eq__` method.
186185

187186
Instances of a class cannot be ordered with respect to other instances of the
188187
same class, or other types of object, unless the class defines enough of the
189-
methods :meth:`__cmp__`, :meth:`__lt__`, :meth:`__le__`, :meth:`__gt__`, and
190-
:meth:`__ge__` (in general, either :meth:`__cmp__` or both :meth:`__lt__` and
191-
:meth:`__eq__` are sufficient, if you want the conventional meanings of the
192-
comparison operators).
188+
methods :meth:`__lt__`, :meth:`__le__`, :meth:`__gt__`, and :meth:`__ge__` (in
189+
general, :meth:`__lt__` and :meth:`__eq__` are sufficient, if you want the
190+
conventional meanings of the comparison operators).
193191

194192
The behavior of the :keyword:`is` and :keyword:`is not` operators cannot be
195193
customized; also they can be applied to any two objects and never raise an
@@ -1642,8 +1640,7 @@ The constructors for both classes work the same:
16421640
The subset and equality comparisons do not generalize to a complete ordering
16431641
function. For example, any two disjoint sets are not equal and are not
16441642
subsets of each other, so *all* of the following return ``False``: ``a<b``,
1645-
``a==b``, or ``a>b``. Accordingly, sets do not implement the :meth:`__cmp__`
1646-
method.
1643+
``a==b``, or ``a>b``.
16471644

16481645
Since sets only define partial ordering (subset relationships), the output of
16491646
the :meth:`list.sort` method is undefined for lists of sets.

Doc/library/xmlrpc.client.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ use by the marshalling/unmarshalling code:
210210
Write the XML-RPC encoding of this :class:`DateTime` item to the *out* stream
211211
object.
212212

213-
It also supports certain of Python's built-in operators through :meth:`__cmp__`
213+
It also supports certain of Python's built-in operators through rich comparison
214214
and :meth:`__repr__` methods.
215215

216216
A working example follows. The server code::
@@ -273,8 +273,8 @@ internal use by the marshalling/unmarshalling code:
273273
which was the de facto standard base64 specification when the
274274
XML-RPC spec was written.
275275

276-
It also supports certain of Python's built-in operators through a
277-
:meth:`__cmp__` method.
276+
It also supports certain of Python's built-in operators through :meth:`__eq__`
277+
and :meth:`__ne__` methods.
278278

279279
Example usage of the binary objects. We're going to transfer an image over
280280
XMLRPC::

Doc/reference/datamodel.rst

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,8 +1168,7 @@ Basic customization
11681168
.. index::
11691169
single: comparisons
11701170

1171-
These are the so-called "rich comparison" methods, and are called for comparison
1172-
operators in preference to :meth:`__cmp__` below. The correspondence between
1171+
These are the so-called "rich comparison" methods. The correspondence between
11731172
operator symbols and method names is as follows: ``x<y`` calls ``x.__lt__(y)``,
11741173
``x<=y`` calls ``x.__le__(y)``, ``x==y`` calls ``x.__eq__(y)``, ``x!=y`` calls
11751174
``x.__ne__(y)``, ``x>y`` calls ``x.__gt__(y)``, and ``x>=y`` calls
@@ -1198,28 +1197,11 @@ Basic customization
11981197
Arguments to rich comparison methods are never coerced.
11991198

12001199

1201-
.. method:: object.__cmp__(self, other)
1202-
1203-
.. index::
1204-
builtin: cmp
1205-
single: comparisons
1206-
1207-
Called by comparison operations if rich comparison (see above) is not
1208-
defined. Should return a negative integer if ``self < other``, zero if
1209-
``self == other``, a positive integer if ``self > other``. If no
1210-
:meth:`__cmp__`, :meth:`__eq__` or :meth:`__ne__` operation is defined, class
1211-
instances are compared by object identity ("address"). See also the
1212-
description of :meth:`__hash__` for some important notes on creating
1213-
:term:`hashable` objects which support custom comparison operations and are
1214-
usable as dictionary keys.
1215-
1216-
12171200
.. method:: object.__hash__(self)
12181201

12191202
.. index::
12201203
object: dictionary
12211204
builtin: hash
1222-
single: __cmp__() (object method)
12231205

12241206
Called for the key object for dictionary operations, and by the built-in
12251207
function :func:`hash`. Should return an integer usable as a hash value
@@ -1228,37 +1210,35 @@ Basic customization
12281210
(e.g., using exclusive or) the hash values for the components of the object that
12291211
also play a part in comparison of objects.
12301212

1231-
If a class does not define a :meth:`__cmp__` or :meth:`__eq__` method it
1232-
should not define a :meth:`__hash__` operation either; if it defines
1233-
:meth:`__cmp__` or :meth:`__eq__` but not :meth:`__hash__`, its instances
1234-
will not be usable as dictionary keys. If a class defines mutable objects
1235-
and implements a :meth:`__cmp__` or :meth:`__eq__` method, it should not
1236-
implement :meth:`__hash__`, since the dictionary implementation requires that
1237-
a key's hash value is immutable (if the object's hash value changes, it will
1238-
be in the wrong hash bucket).
1213+
If a class does not define an :meth:`__eq__` method it should not define a
1214+
:meth:`__hash__` operation either; if it defines :meth:`__eq__` but not
1215+
:meth:`__hash__`, its instances will not be usable as dictionary keys. If a
1216+
class defines mutable objects and implements an :meth:`__eq__` method, it
1217+
should not implement :meth:`__hash__`, since the dictionary implementation
1218+
requires that a key's hash value is immutable (if the object's hash value
1219+
changes, it will be in the wrong hash bucket).
12391220

1240-
User-defined classes have :meth:`__cmp__` and :meth:`__hash__` methods
1221+
User-defined classes have :meth:`__eq__` and :meth:`__hash__` methods
12411222
by default; with them, all objects compare unequal (except with themselves)
12421223
and ``x.__hash__()`` returns ``id(x)``.
12431224

12441225
Classes which inherit a :meth:`__hash__` method from a parent class but
1245-
change the meaning of :meth:`__cmp__` or :meth:`__eq__` such that the hash
1246-
value returned is no longer appropriate (e.g. by switching to a value-based
1247-
concept of equality instead of the default identity based equality) can
1248-
explicitly flag themselves as being unhashable by setting
1249-
``__hash__ = None`` in the class definition. Doing so means that not only
1250-
will instances of the class raise an appropriate :exc:`TypeError` when
1251-
a program attempts to retrieve their hash value, but they will also be
1252-
correctly identified as unhashable when checking
1253-
``isinstance(obj, collections.Hashable)`` (unlike classes which define
1254-
their own :meth:`__hash__` to explicitly raise :exc:`TypeError`).
1255-
1256-
If a class that overrrides :meth:`__cmp__` or :meth:`__eq__` needs to
1257-
retain the implementation of :meth:`__hash__` from a parent class,
1258-
the interpreter must be told this explicitly by setting
1259-
``__hash__ = <ParentClass>.__hash__``. Otherwise the inheritance of
1260-
:meth:`__hash__` will be blocked, just as if :attr:`__hash__` had been
1261-
explicitly set to :const:`None`.
1226+
change the meaning of :meth:`__eq__` such that the hash value returned is no
1227+
longer appropriate (e.g. by switching to a value-based concept of equality
1228+
instead of the default identity based equality) can explicitly flag
1229+
themselves as being unhashable by setting ``__hash__ = None`` in the class
1230+
definition. Doing so means that not only will instances of the class raise an
1231+
appropriate :exc:`TypeError` when a program attempts to retrieve their hash
1232+
value, but they will also be correctly identified as unhashable when checking
1233+
``isinstance(obj, collections.Hashable)`` (unlike classes which define their
1234+
own :meth:`__hash__` to explicitly raise :exc:`TypeError`).
1235+
1236+
If a class that overrrides :meth:`__eq__` needs to retain the implementation
1237+
of :meth:`__hash__` from a parent class, the interpreter must be told this
1238+
explicitly by setting ``__hash__ = <ParentClass>.__hash__``. Otherwise the
1239+
inheritance of :meth:`__hash__` will be blocked, just as if :attr:`__hash__`
1240+
had been explicitly set to :const:`None`.
1241+
12621242

12631243
.. method:: object.__bool__(self)
12641244

0 commit comments

Comments
 (0)