Message311643
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
pprint.pprint(visited.most_common(20))
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. |
|
Date |
User |
Action |
Args |
2018-02-05 04:36:16 | rhettinger | set | recipients:
+ rhettinger, docs@python, anthony-flury |
2018-02-05 04:36:16 | rhettinger | set | messageid: <1517805376.67.0.467229070634.issue32770@psf.upfronthosting.co.za> |
2018-02-05 04:36:16 | rhettinger | link | issue32770 messages |
2018-02-05 04:36:15 | rhettinger | create | |
|