[Python-checkins] python/dist/src/Lib sets.py,1.12,1.13
gvanrossum@users.sourceforge.net
gvanrossum@users.sourceforge.net
Thu, 22 Aug 2002 10:23:37 -0700
Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv14729
Modified Files:
sets.py
Log Message:
Change the binary operators |, &, ^, - to return NotImplemented rather
than raising TypeError when the other argument is not a BaseSet. This
made it necessary to separate the implementation of e.g. __or__ from
the union method; the latter should not return NotImplemented but
raise TypeError. This is accomplished by making union(self, other)
return self|other, etc.; Python's binary operator machinery will raise
TypeError.
The idea behind this change is to allow other set implementations with
an incompatible internal structure; these can provide union (etc.) with
standard sets by implementing __ror__ etc.
I wish I could do this for comparisons too, but the default comparison
implementation allows comparing anything to anything else (returning
false); we don't want that (at least the test suite makes sure
e.g. Set()==42 raises TypeError). That's probably fine; otherwise
other set implementations would be constrained to implementing a hash
that's compatible with ours.
Index: sets.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/sets.py,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** sets.py 21 Aug 2002 13:20:51 -0000 1.12
--- sets.py 22 Aug 2002 17:23:33 -0000 1.13
***************
*** 54,58 ****
#
# - Raymond Hettinger added a number of speedups and other
! # bugs^H^H^H^Himprovements.
--- 54,58 ----
#
# - Raymond Hettinger added a number of speedups and other
! # improvements.
***************
*** 156,179 ****
return result
! # Standard set operations: union, intersection, both differences
! def union(self, other):
"""Return the union of two sets as a new set.
(I.e. all elements that are in either set.)
"""
! self._binary_sanity_check(other)
result = self.__class__(self._data)
result._data.update(other._data)
return result
! __or__ = union
! def intersection(self, other):
"""Return the intersection of two sets as a new set.
(I.e. all elements that are in both sets.)
"""
! self._binary_sanity_check(other)
if len(self) <= len(other):
little, big = self, other
--- 156,188 ----
return result
! # Standard set operations: union, intersection, both differences.
! # Each has an operator version (e.g. __or__, invoked with |) and a
! # method version (e.g. union).
! def __or__(self, other):
"""Return the union of two sets as a new set.
(I.e. all elements that are in either set.)
"""
! if not isinstance(other, BaseSet):
! return NotImplemented
result = self.__class__(self._data)
result._data.update(other._data)
return result
! def union(self, other):
! """Return the union of two sets as a new set.
! (I.e. all elements that are in either set.)
! """
! return self | other
!
! def __and__(self, other):
"""Return the intersection of two sets as a new set.
(I.e. all elements that are in both sets.)
"""
! if not isinstance(other, BaseSet):
! return NotImplemented
if len(self) <= len(other):
little, big = self, other
***************
*** 188,199 ****
return result
! __and__ = intersection
! def symmetric_difference(self, other):
"""Return the symmetric difference of two sets as a new set.
(I.e. all elements that are in exactly one of the sets.)
"""
! self._binary_sanity_check(other)
result = self.__class__([])
data = result._data
--- 197,214 ----
return result
! def intersection(self, other):
! """Return the intersection of two sets as a new set.
! (I.e. all elements that are in both sets.)
! """
! return self & other
!
! def __xor__(self, other):
"""Return the symmetric difference of two sets as a new set.
(I.e. all elements that are in exactly one of the sets.)
"""
! if not isinstance(other, BaseSet):
! return NotImplemented
result = self.__class__([])
data = result._data
***************
*** 207,218 ****
return result
! __xor__ = symmetric_difference
! def difference(self, other):
"""Return the difference of two sets as a new Set.
(I.e. all elements that are in this set and not in the other.)
"""
! self._binary_sanity_check(other)
result = self.__class__([])
data = result._data
--- 222,239 ----
return result
! def symmetric_difference(self, other):
! """Return the symmetric difference of two sets as a new set.
! (I.e. all elements that are in exactly one of the sets.)
! """
! return self ^ other
!
! def __sub__(self, other):
"""Return the difference of two sets as a new Set.
(I.e. all elements that are in this set and not in the other.)
"""
! if not isinstance(other, BaseSet):
! return NotImplemented
result = self.__class__([])
data = result._data
***************
*** 223,227 ****
return result
! __sub__ = difference
# Membership test
--- 244,253 ----
return result
! def difference(self, other):
! """Return the difference of two sets as a new Set.
!
! (I.e. all elements that are in this set and not in the other.)
! """
! return self - other
# Membership test