Friday, 13 September 2013

maximum recursion depth exceeded, overriding methods in new class under UserList

maximum recursion depth exceeded, overriding methods in new class under
UserList

I am creating a new class in UserList, and trying to override the add,
append, and extend methods so that duplicate values will not be added to
the list by any of these operations. So far I have started with trying to
override the append method and when I try to implement the class on an
object I get the error: maximum recursion depth exceeded. Here's what I
have so far:
from collections import UserList
class UList(UserList):
def append(self,item):
for s in self:
if item == s:
print ("Item already exists in list")
else:
self.append(item)
x = [1,2,3,4,5]
z = UList(x)
print (z)
z.append(1)

No comments:

Post a Comment