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

Skip to content

Commit 00d43fd

Browse files
committed
Fix doctest to not rely on order of dictionary entries.
Use super() instead of direct references to the dict superclass.
1 parent 345c49b commit 00d43fd

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

Lib/collections.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -366,25 +366,25 @@ class Counter(dict):
366366
or multiset. Elements are stored as dictionary keys and their counts
367367
are stored as dictionary values.
368368
369-
>>> c = Counter('abracadabra') # count elements from a string
369+
>>> c = Counter('abcdeabcdabcaba') # count elements from a string
370370
371371
>>> c.most_common(3) # three most common elements
372-
[('a', 5), ('r', 2), ('b', 2)]
372+
[('a', 5), ('b', 4), ('c', 3)]
373373
>>> sorted(c) # list all unique elements
374-
['a', 'b', 'c', 'd', 'r']
374+
['a', 'b', 'c', 'd', 'e']
375375
>>> ''.join(sorted(c.elements())) # list elements with repetitions
376-
'aaaaabbcdrr'
376+
'aaaaabbbbcccdde'
377377
>>> sum(c.values()) # total of all counts
378-
11
378+
15
379379
380380
>>> c['a'] # count of letter 'a'
381381
5
382382
>>> for elem in 'shazam': # update counts from an iterable
383383
... c[elem] += 1 # by adding 1 to each element's count
384384
>>> c['a'] # now there are seven 'a'
385385
7
386-
>>> del c['r'] # remove all 'r'
387-
>>> c['r'] # now there are zero 'r'
386+
>>> del c['b'] # remove all 'b'
387+
>>> c['b'] # now there are zero 'b'
388388
0
389389
390390
>>> d = Counter('simsalabim') # make another counter
@@ -423,6 +423,7 @@ def __init__(self, iterable=None, **kwds):
423423
>>> c = Counter(a=4, b=2) # a new counter from keyword args
424424
425425
'''
426+
super().__init__()
426427
self.update(iterable, **kwds)
427428

428429
def __missing__(self, key):
@@ -434,8 +435,8 @@ def most_common(self, n=None):
434435
'''List the n most common elements and their counts from the most
435436
common to the least. If n is None, then list all element counts.
436437
437-
>>> Counter('abracadabra').most_common(3)
438-
[('a', 5), ('r', 2), ('b', 2)]
438+
>>> Counter('abcdeabcdabcaba').most_common(3)
439+
[('a', 5), ('b', 4), ('c', 3)]
439440
440441
'''
441442
# Emulate Bag.sortedByCount from Smalltalk
@@ -501,7 +502,7 @@ def update(self, iterable=None, **kwds):
501502
for elem, count in iterable.items():
502503
self[elem] = count + self_get(elem, 0)
503504
else:
504-
dict.update(self, iterable) # fast path when counter is empty
505+
super().update(iterable) # fast path when counter is empty
505506
else:
506507
_count_elements(self, iterable)
507508
if kwds:
@@ -541,7 +542,7 @@ def copy(self):
541542
def __delitem__(self, elem):
542543
'Like dict.__delitem__() but does not raise KeyError for missing values.'
543544
if elem in self:
544-
dict.__delitem__(self, elem)
545+
super().__delitem__(elem)
545546

546547
def __repr__(self):
547548
if not self:

0 commit comments

Comments
 (0)