[Python-3000] UserDict revamp

Guido van Rossum guido at python.org
Thu Feb 15 18:15:56 CET 2007


On 2/15/07, Steven Bethard <steven.bethard at gmail.com> wrote:
> On 2/15/07, Guido van Rossum <guido at python.org> wrote:
> > Perhaps the most egregious example is MixinDict, which currently
> > assumes that keys() is a primitive operation returning a list, and
> > builds __iter__() out of that. Obviously a better approach is to turn
> > this around. (I'd have thought that ever since 2.2 this would have
> > been the better design, but perhaps it was too late then already.)
>
> I asked the same thing back in early 2005:
>
>     http://mail.python.org/pipermail/python-list/2005-January/300042.html
>
> Glad to hear I wasn't too out of my mind. ;-)

Reading that post, I think that __len__ should also be part of the
primitive operations, at least optionally. The dict view code to
compare two views (or a view and a set; always excluding the values
view which is not a set) for equality makes good use of this since it
knows that if the lengths are unequal the objects cannot be equal. In
order to determine equality without knowing the legth would double the
cost of the operation because you'd end up having to iterate over each
side, checking that all its elements are contained in the other side.
With a length check, you only have to iterate over one side, and only
if the lengths are equal.

Another distinction I'd like to make is between mutable and immutable
mappings. But maybe this is outside the realm of a *dict* mixin, and
belongs in the (more speculative) discussion on abstract base classes.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list