[Tutor] subclass of list

Michael Janssen Janssen@rz.uni-frankfurt.de
Fri Jan 3 18:48:02 2003


On Fri, 3 Jan 2003, Bob Gailer wrote:

> In the latest Python one can subclass types, such as list.
> What is the benefit? What can one do with an instance of a subclass of list?

I made very slow processes in understanding those OOP stuff, but one of
the first things I've done with it was subclassing dictionaries and
strings (sorry, not lists, but it's the same: adaptation of datastructures
to personal needs).

As a beginnner I don't subclass dict/list/string directly but the
UserDict/List/String modules: These classes reimplement the exact
behaviour of dict-list-strings. When you modify (subclass) these classes
you possibly don't overwrite some essential functionality (Because you
can take a look at the functionality in UserDict/andsoon).

If subclassed UserDict in order to provide some more methods especially
for the needs of my programm: it uses an internal "data-container" which
is in core a dict (i.e. was a dict in early programm-versions). I
recognize that I was constantly doing the same jobs with this data
containing dict and so I decided to enhance UserDict with methods for this
jobs. My life was easier since then, because I needn't to paste and copy
the code any more but use the new methods (which means: I needn't to think
of the internal implementation any more)

Some days ago I have added a "readline" methods to UserString: I was
struggling with the Classes in mailbox, mimetools and multifile moduls
which all want to get a filedescriptor to read from. Instantiate those
Classes with strings fails because of the missing readline method. Now it
*works* (Disclaimer: this was purely to learn how thoses Classes work; I
have removed this "workaround-code" and go on with real filedescriptors.)

For lists, it can be: You don't want to write median(AList) (1) but
rather:  AList.median(). Or you want to compair two list not in the
implementet way (That is campaire the first values, if equally the seconds
and so on) but for example by its median. therefore you simply overwrite
the __cmp__ method. Or you want to get something completly different as a
two dimensional array. Perhaps subclassing list or UserList is a good
start point for this.

In short: You can put additional methods into your own classes and forget
about the implementation.
You can use __operator__ methods (look in
doc/lib/module-operator.html for this) to call methods "without calling
them directly" (hope you understand what I mean). With these operators you
can overwrite the *behaviour* of list/dict/string

On a side note: I've got my first inside in what OOP is for, as someone
wrotes that "OOP means to put the data and functionality together". From
then on, I rewrote my script, which consists of functions to handel input
and store it into datastructures and functions to retrieve the data and
transform it for output. now this script consist of a modified UserDict
which makes much of the work self and functions to call my UserDict
instance (that's not OOP, but it's a step, I believe ;-). Therefore I'm
comming from the datastructure side and go to put some functionality into
data, which might be a kind of OOP; but: only one kind of ;-)

Michael

> I'd like to think that, given:
>
> class MyList(list):
>    def __init__(self, list)
>      self.list = list
> x = MyList([3,4])
>
> x[0] would then return 3?

When you take a look into UserList.py you might find out yourself, why
this isn't already working ;-)
>
> Bob Gailer
> mailto:ramrom@earthling.net
> 303 442 2625
>