[Tutor] Ingenious script (IMO)

Dick Moores rdm at rcblue.com
Mon Aug 6 17:43:50 CEST 2007


At 07:44 AM 8/6/2007, Kent Johnson wrote:
>Dick Moores wrote:
>>Gotta say though, I still don't understand how the defaultdict works here.
>
>Did you try the docs?
>http://docs.python.org/lib/defaultdict-objects.html

Yes, but it left me still in the dark.


>If coinCount were an ordinary dict, the line
>   coinCount[deno] += 1
>
>would have to be written as
>   coinCount[deno] = coinCount.get(deno, 0) + 1
>
>Using defaultdict(int) makes the use of 0 as the default value 
>automatic. (Actually the default value is obtained by calling int(), 
>whose value is 0.)

OK, thanks for spelling it out, Kent.

Dick



More information about the Tutor mailing list