Pre-PEP: Dictionary accumulator methods

Paul Rubin http
Sun Mar 20 09:20:33 CET 2005


Mike Rovner <mrovner at propel.com> writes:
> It can be tipped like that:
> 
> di = dict(int)
> di.setdefault(0)
> di[key] += 1
...
> But the point is that if method not found in dict it delegated to
> container type specified in constructor.
> 
> It solves dict specialization without bloating dict class and is generic.

Hey, I like that.  I'd let the default be an optional extra arg to the
constructor:

   di = dict(int, default=0)
   di[key] += 1

without the setdefault.  I might even add optional type checking:

  di = dict(int, default=0, typecheck=True)
  di[key] = 'foo'    # raises TypeError



More information about the Python-list mailing list