Pre-PEP: Dictionary accumulator methods - typing & initialising

Kay Schluehr kay.schluehr at gmx.net
Sun Mar 20 05:46:33 EST 2005


Duncan Booth wrote:
> Raymond Hettinger wrote:
>
> > The rationale is to replace the awkward and slow existing idioms
for
> > dictionary based accumulation:
> >
> >     d[key] = d.get(key, 0) + qty
> >     d.setdefault(key, []).extend(values)
> >
>
> How about the alternative approach of allowing the user to override
the
> action to be taken when accessing a non-existent key?
>
>    d.defaultValue(0)
>
> and the accumulation becomes:
>
>    d[key] += 1
>
> and:
>
>    d.defaultValue(function=list)
>
> would allow a safe:
>
>   d[key].extend(values)

+0

The best suggestion up to now. But i find this premature because it
addresses only a special aspect of typing issues which should be
disussed together with Guidos type guard proposals in a broader
context. Besides this the suggestion though feeling pythonic is still
uneven.

Why do You set

d.defaultValue(0)
d.defaultValue(function=list)

but not

d.defaultValue(0)
d.defaultValue([])

?

And why not dict(type=int), dict(type=list) instead where default
values are instantiated during object creation? A consistent pythonic
handling of all types should be envisioned not some ad hoc solutions
that go deprecated two Python releases later.

Regards Kay




More information about the Python-list mailing list