Overriding UserList methods

Chris Gonnerman chris.gonnerman at usa.net
Tue Mar 6 21:31:40 EST 2001


----- Original Message -----
From: "Daniel Klein" <danielk at aracnet.com>
Subject: Re: Overriding UserList methods
> On Mon, 5 Mar 2001 00:52:40 -0500, "Steve Holden" <sholden at holdenweb.com>
> wrote:
>
> >"Daniel Klein" <danielk at aracnet.com> wrote in message
> >news:b566atck0ruu4tae8n1srrkntbmme26gor at 4ax.com...
> >> I must be missing something, cos I can't figure out how to override a
> >method
> >> using the UserList module. Here is some code:
> >>
> >> import UserList
> >> class MyList(UserList.UserList):
> >>       __def __init__(self):
> >>         UserList.UserList.__init__(self)
> >>
> >While this would work, I don't see it's strictly necessary. Why not just
> >inherit the __init__ method from UserList?
>
> One of the reasons for subclassing UserList is to provide additional
instance
> variables. So I need my own __init__ method to initialize this, don't I?

Nope.  Only if you *actually* initialize them in the __init__ method, which
you aren't doing.

I wrote a module a while back that subclasses UserDict... not exactly the
same
problem but close enough to help.  If you are interested, visit

    http://newcenturycomputers.net/projects/python

and download the Chunk.py module.

Remember that when you call a method of an instance variable, i.e.:

    from UserList import UserList
    lst = UserList()
    lst.append(1)

what is (effectively) called is:

    UserList.append(lst, 1)

So:

    class MyList(UserList):
        def append(self, item):
            UserList.append(self, item)

is the "right" way to do it.  Of course, you can also just use the .append()
method of the underlying .data element, but I prefer to use the "proper"
encapsulation.







More information about the Python-list mailing list