Where's UserList.ListMixin?

Raymond Hettinger vze4rx4y at verizon.net
Thu Sep 18 07:18:25 CEST 2003


[Matthew Barnes]
> This may be a naive question, but since Python 2.3 added a handy
> little DictMixin class to its UserDict module it seems to me like
> UserList.ListMixin and maybe even UserString.StringMixin should have
> followed (although I'm not sure how useful a StringMixin class would
> really be).

Use cases have to come first.  Do you see modules in the library
or in common applications where this has immediate use?
When I developed DickNixon, er, DictMixin (not the former President),
there were immediate applications in shelve, dumbdbm, and some of
my own apps.

As a starting point, look in calendar.py to see whether your idea
would have helped implement _localized_day and _localized_month.
I think these classes are typical of objects that try to dynamically
simulate list behavior.


>  Should I submit a patch?

It is best to start by submitting a recipe to the ASPN Cookbook.
There, the idea can be refined, use cases established, and a fan
club formed.   At that point, it could be a candidate for inclusion
in Py2.4.

It may be best to start which an easier challenge like TupleMixin
and then add mutuable behaviors to a ListMixin subclass.  An
even easier start is to build a RichComparisonMixin that
transforms == and < into <=, >, >=, and !=.

If you want to show-off your agile programming skills, develop
the unittest cases before you write your code.

The most important part of the design is thinking out which methods
should be the primitives and whether there should be multiple
levels so that the most natural overrides get used by higher levels
(see the DictMixin code if you don't know what I'm talking about
here).  Try to keep your thinking grounded in reality by examining
real modules which would benefit from subclassing the mixins.

Also, I'm curious as to how you would implement the sort()
method without actually manifesting the whole virtual list;
otherwise, you might as well use list(iterable) and get a real list
(which, BTW, is what _localized_day does using a list
comprehension).

Good luck with your project,


Raymond Hettinger






More information about the Python-list mailing list