[Tutor] Overloading in UserList - Corrected

Daniel Yoo dyoo@hkn.EECS.Berkeley.EDU
Sat, 8 Jul 2000 13:04:13 -0700 (PDT)

On Sat, 8 Jul 2000, jazariel@libero.it wrote:

> I just discovered that the Linux e-mail client kmail
> lost all the identation. Hope it works from the web.

It didn't quite work --- the web email slightly mangled your
ensure() method:

>     def ensure(self, value):
>         """ Method to add a value if it is not already in list """
> 	if not value in self.data:
>     	self.data.append(value)

But in any case, we need to look at part of UserList, specifically, the
__getslice__() method to see why this is happening:

    def __getslice__(self, i, j):
        i = max(i, 0); j = max(j, 0)
        userlist = self.__class__()
        userlist.data[:] = self.data[i:j]
        return userlist

So it's creating a new instance of the list called userlist, and then
after that it copies the data.  This is where the code is breaking.  The
reason is because your __init__ method in TestList works only if you pass
it a list --- you need to make it work so that:

  mylist = TestList()

is possible.  The way that they made it work in UserList is to give the
parameter a default value:

    def __init__(self, list=None):

It should be a quick fix to have your TestList have similar
behavior.  Good luck!