Issue 8743: Improve interoperability between sets and the collections.Set abstract base class.
diff --git a/Lib/_abcoll.py b/Lib/_abcoll.py
index 8b650a7..a943263 100644
--- a/Lib/_abcoll.py
+++ b/Lib/_abcoll.py
@@ -165,12 +165,17 @@
def __gt__(self, other):
if not isinstance(other, Set):
return NotImplemented
- return other.__lt__(self)
+ return len(self) > len(other) and self.__ge__(other)
def __ge__(self, other):
if not isinstance(other, Set):
return NotImplemented
- return other.__le__(self)
+ if len(self) < len(other):
+ return False
+ for elem in other:
+ if elem not in self:
+ return False
+ return True
def __eq__(self, other):
if not isinstance(other, Set):
@@ -194,6 +199,8 @@
return NotImplemented
return self._from_iterable(value for value in other if value in self)
+ __rand__ = __and__
+
def isdisjoint(self, other):
'Return True if two sets have a null intersection.'
for value in other:
@@ -207,6 +214,8 @@
chain = (e for s in (self, other) for e in s)
return self._from_iterable(chain)
+ __ror__ = __or__
+
def __sub__(self, other):
if not isinstance(other, Set):
if not isinstance(other, Iterable):
@@ -215,6 +224,14 @@
return self._from_iterable(value for value in self
if value not in other)
+ def __rsub__(self, other):
+ if not isinstance(other, Set):
+ if not isinstance(other, Iterable):
+ return NotImplemented
+ other = self._from_iterable(other)
+ return self._from_iterable(value for value in other
+ if value not in self)
+
def __xor__(self, other):
if not isinstance(other, Set):
if not isinstance(other, Iterable):
@@ -222,6 +239,8 @@
other = self._from_iterable(other)
return (self - other) | (other - self)
+ __rxor__ = __xor__
+
# Sets are not hashable by default, but subclasses can change this
__hash__ = None