[Tutor] Should I subclass UserDict or dict?

Terry Carroll carroll@tjc.com
Fri May 9 22:14:01 2003


I'm writing a little class that provides an easy-to-use interface into the
Unicode Chinese character database as set out in Unicode.org's Unihan.txt
(online at http://www.unicode.org/Public/UNIDATA/Unihan.txt); The file's a
bit over 25M, so re-parsing it each time you want to look up something
isn't practical.

I started out doing this as a dictionary of dictionaries, but there are a 
few things that make me want to make it its own class (for example, 
capturing the comments in the file, which document it nicely, and allowing 
an easy interface to search for a particular value of an attribute).

This is primarily for my own use, but I plan on making it generally
available just in case anyone else can use it, too.  My question (you knw
there was a question eventually, right?): Should I inherit from the
built-in dict class or from UserDict?

On the only platform I plan to run this on, I run Python 2.2.2.  I know 
that dict is only inheritable from starting in 2.2.  Is 2.2 pervasive now 
(my ISP's highest level of Python is 2.1.1, for example), or should I use 
UserDict to ensure compatibility?  Is there really any big advantage of 
user the built-in dict instead of UserDict?  

The docs for UserDict say "If you are writing code that does not need to
work with versions of Python earlier than Python 2.2, please consider
subclassing directly from the built-in dict type."  Since it says 
"please," I thought I should.

-- 
Terry Carroll        |  "To have this rare opportunity
Santa Clara, CA      |    is a rare opportunity."
carroll@tjc.com      |    - Houston Rockets' Yao Ming, on being named
Modell delendus est  |    starting center for the 2003 NBA All-Star Game