[Tutor] Overloading in UserList
Thomas Schulze
jazariel@libero.it
Sat, 8 Jul 2000 14:56:55 +0200
Ciao,
after reading the book 'Learning Python', I started to write a simple game
programm.
I extended the UserList with some simple new methods which seem to work
fine. However I lost the slicing capability for the list.
I am curious what the reson might be.
Thomas
_____________________________________________________________
The following code:
from UserList import UserList
class TestList(UserList):
""" Class TestList based on class UserList """
def __init__(self, list):
UserList.__init__(self, list)
def purge(self, value):
""" Method to check if a value is in list and remove all its entries """
while value in self.data:
self.data.remove(value)
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)
def copy(self):
""" Makes a copy of the instance """
return TestList(self.data[:])
produced after loading it:
>>> a = [1,2,3,4]
>>> A1 = UserList(a)
>>> A2 = TestList(a)
>>> A1[:]
[1, 2, 3, 4]
>>> A2[:]
Traceback (innermost last):
File "<pyshell#14>", line 1, in ?
A2[:]
File "C:\Programmi\Python\Lib\UserList.py", line 23, in __getslice__
userlist = self.__class__()
TypeError: not enough arguments; expected 2, got 1
>>> A2.copy()
[1, 2, 3, 4]