[docs] [issue32770] collections.counter examples are misleading
report at bugs.python.org
Sun Feb 4 23:36:16 EST 2018
Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:
Thanks for the suggestion. I respectfully disagree. The "core" functionality of Counter is the ability to write c['x'] += 1 without risking a KeyError. The add-on capability is to process an entire iterable all at once. This is analogous to the list() builtin- where the core ability is to write s.append(e) and there is a convenience of calling list(iterable).
Another reason the first example goes first because it is simple. It shows counting in isolation with no other distractions (an in-vitro example).
The second example is in a more complex environment incorporating file access and regular expressions (an in-vivo example).
FWIW, there are plenty of examples of using the += style. Here's one I use in my Python courses:
'Scan a log file from a NASA server'
import collections, re, pprint
visited = collections.Counter()
with open('notes/nasa_19950801.log') as f:
for line in f:
mo = re.search(r'GET\s+(\S+)\s+200', line)
if mo is not None:
url = mo.group(1)
visited[url] += 1
I've had good luck with people understanding the docs as-is, so I'm going to decline the suggestion. I do appreciate you taking the time to share your thoughts.
assignee: docs at python -> rhettinger
resolution: -> not a bug
stage: -> resolved
status: open -> closed
Python tracker <report at bugs.python.org>
More information about the docs