[Python-checkins] r54612 - sandbox/trunk/abc/abc.py

guido.van.rossum python-checkins at python.org
Fri Mar 30 02:49:46 CEST 2007


Author: guido.van.rossum
Date: Fri Mar 30 02:49:45 2007
New Revision: 54612

Modified:
   sandbox/trunk/abc/abc.py
Log:
Add Sizeable base class and comparisons to Sequence.
(Do we need ABCs to indicate comparability?)


Modified: sandbox/trunk/abc/abc.py
==============================================================================
--- sandbox/trunk/abc/abc.py	(original)
+++ sandbox/trunk/abc/abc.py	Fri Mar 30 02:49:45 2007
@@ -322,7 +322,7 @@
 ### SEQUENCES ###
 
 
-class Sequence(Iterable):
+class Sequence(Sizeable, Iterable):
 
   """A minimal sequence.
 
@@ -406,3 +406,32 @@
       return NotImplemented
     repeat = self.__index(repeat)
     return self.__class__(elem for i in range(repeat) for elem in self)
+
+  def __eq__(self, other):
+    if not isinstance(other, Sequence):
+      return NotImplemented
+    if len(self) != len(other):
+      return False
+    for a, b in zip(self, other):
+      if a == b:
+        continue
+      return False
+    return len(self) == len(other)
+
+  def __lt__(self, other):
+    if not isinstance(other, Sequence):
+      return NotImplemented
+    for a, b in zip(self, other):
+      if a == b:
+        continue
+      return a < b
+    return len(self) < len(other)
+
+  def __le__(self, other):
+    if not isinstance(other, Sequence):
+      return NotImplemented
+    for a, b in zip(self, other):
+      if a == b:
+        continue
+      return a < b
+    return len(self) <= len(other)


More information about the Python-checkins mailing list