Overriding UserList methods

Steve Holden sholden at holdenweb.com
Tue Mar 6 19:27:23 EST 2001


"Daniel Klein" <danielk at aracnet.com> wrote in message
news:qqu9atsitacnuacu29k25rvtrieofeg6nk at 4ax.com...
> 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?
>
Only if the instance variables have to be around from instance creation
onwards. They might otherwise be created by other methods. Perhaps you just
abbreviated your code?

[ ... ]

>>> class MyUL(UserList.UserList):
...  def addvar(self, val):
...   self.var = val
...  def showvar(self):
...   print self.var
...
>>> ul = MyUL([1,2,3,4,5])
>>> ul.addvar(42)
>>> ul.showvar()
42
>>> ul
[1, 2, 3, 4, 5]
>>>

regards
 Steve






More information about the Python-list mailing list