]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Minor code rearrangement to group related methods together. (GH-30813)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Sun, 23 Jan 2022 00:47:22 +0000 (18:47 -0600)
committerGitHub <noreply@github.com>
Sun, 23 Jan 2022 00:47:22 +0000 (18:47 -0600)
* Make example more focused with math.prod()
* Move comparison tests to the multiset operations section

Lib/collections/__init__.py

index d989d85d6d829382a61c13350664dedb2b1a9075..fa8b30985a43502561ce3903db69f9d118f147f1 100644 (file)
@@ -617,11 +617,9 @@ class Counter(dict):
         ['A', 'A', 'B', 'B', 'C', 'C']
 
         # Knuth's example for prime factors of 1836:  2**2 * 3**3 * 17**1
+        >>> import math
         >>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
-        >>> product = 1
-        >>> for factor in prime_factors.elements():     # loop over factors
-        ...     product *= factor                       # and multiply them
-        >>> product
+        >>> math.prod(prime_factors.elements())
         1836
 
         Note, if an element's count has been set to zero or is a negative
@@ -718,42 +716,6 @@ class Counter(dict):
         if elem in self:
             super().__delitem__(elem)
 
-    def __eq__(self, other):
-        'True if all counts agree. Missing counts are treated as zero.'
-        if not isinstance(other, Counter):
-            return NotImplemented
-        return all(self[e] == other[e] for c in (self, other) for e in c)
-
-    def __ne__(self, other):
-        'True if any counts disagree. Missing counts are treated as zero.'
-        if not isinstance(other, Counter):
-            return NotImplemented
-        return not self == other
-
-    def __le__(self, other):
-        'True if all counts in self are a subset of those in other.'
-        if not isinstance(other, Counter):
-            return NotImplemented
-        return all(self[e] <= other[e] for c in (self, other) for e in c)
-
-    def __lt__(self, other):
-        'True if all counts in self are a proper subset of those in other.'
-        if not isinstance(other, Counter):
-            return NotImplemented
-        return self <= other and self != other
-
-    def __ge__(self, other):
-        'True if all counts in self are a superset of those in other.'
-        if not isinstance(other, Counter):
-            return NotImplemented
-        return all(self[e] >= other[e] for c in (self, other) for e in c)
-
-    def __gt__(self, other):
-        'True if all counts in self are a proper superset of those in other.'
-        if not isinstance(other, Counter):
-            return NotImplemented
-        return self >= other and self != other
-
     def __repr__(self):
         if not self:
             return f'{self.__class__.__name__}()'
@@ -795,6 +757,42 @@ class Counter(dict):
     #         (cp >= cq) == (sp >= sq)
     #         (cp > cq) == (sp > sq)
 
+    def __eq__(self, other):
+        'True if all counts agree. Missing counts are treated as zero.'
+        if not isinstance(other, Counter):
+            return NotImplemented
+        return all(self[e] == other[e] for c in (self, other) for e in c)
+
+    def __ne__(self, other):
+        'True if any counts disagree. Missing counts are treated as zero.'
+        if not isinstance(other, Counter):
+            return NotImplemented
+        return not self == other
+
+    def __le__(self, other):
+        'True if all counts in self are a subset of those in other.'
+        if not isinstance(other, Counter):
+            return NotImplemented
+        return all(self[e] <= other[e] for c in (self, other) for e in c)
+
+    def __lt__(self, other):
+        'True if all counts in self are a proper subset of those in other.'
+        if not isinstance(other, Counter):
+            return NotImplemented
+        return self <= other and self != other
+
+    def __ge__(self, other):
+        'True if all counts in self are a superset of those in other.'
+        if not isinstance(other, Counter):
+            return NotImplemented
+        return all(self[e] >= other[e] for c in (self, other) for e in c)
+
+    def __gt__(self, other):
+        'True if all counts in self are a proper superset of those in other.'
+        if not isinstance(other, Counter):
+            return NotImplemented
+        return self >= other and self != other
+
     def __add__(self, other):
         '''Add counts from two counters.