Generally most use this: freqs = {}
for c in "abracadabra":
try:
freqs[c] += 1
except:
freqs[c] = 1
If this is really the common idiom, I'd say this is a sign that professional programming has yet to fully mature as a field.
Some may say a better solution would be:
freqs = {}
for c in "abracadabra":
freqs[c] = freqs.get(c, 0) + 1
Okay, so I understood immediately what was going on with the 2nd bit of code.
Rather go for the collection type defaultdict
from collections import defaultdict
freqs = defaultdict(int)
for c in "abracadabra":
freqs[c] += 1
As a non-pythonista, the 3rd bit of code, I had to Google "defaultdict" to figure out. It's only a couple of seconds to Google, and a professional should know this tidbit, but it seems like premature optimization to me. This brings to mind this post:
http://news.ycombinator.com/item?id=3995185
As a programmer, one's most valuable resource is brainpower. Supposedly, a programmer's most important goal is writing clear code. Look around at what goes on in our industry. There's a lot of our most valuable resource spent on showing off our cleverness, not directed towards the clearest code. To me this is like spending money to show one can spend money or playing an instrument to show off dexterity instead of producing gorgeous sounds.
(I think this starts in school and other environments where one is motivated to show off one's coding chops.)
Most of the complexity in our field accrues like litter: a bit here and a bit there. I think it says something about the culture of the folks who live there.