[Tutor] critique my wrapper

Andre Roberge andre.roberge at gmail.com
Thu Feb 2 20:55:34 CET 2006


On 2/2/06, Christopher Spears <cspears2002 at yahoo.com> wrote:
> This is a class I created that wraps a list.  Could
> someone please critique the class?
>
> class MyList:
>         def __init__(self, aList=None):
>                 if aList is None:
>                         self.mylist = []
>                 else:
>                         self.mylist = aList[:]
>         def __getitem__(self, index):
>                 return self.mylist[index]
>         def __setitem__(self, index, value):
>                 self.mylist[index] = value
>         def __len__(self):
>                 return len(self.mylist)
>         def __delitem__(self, index):
>                 del self.mylist[index]
>         def __add__(self, other):
>                 self.mylist = self.mylist + other
>         def __repr__(self):
>                 return '%s' % self.mylist
>         def append(self, other):
[snip]

It's hard to critique without knowing your intentions.  Here's a
simpler class that should
do the same as yours!

class MyList2(list):
    def __init__(self, aList=[]):
         list(aList)

I may have missed something but, unless you are re-defining a list
method, it is easier to simply subclass list like I have done - and
only re-define the methods whose behaviour you want to redefine, or
add new method.  Otherwise, simply use

a_list = list()  !!!

Then again, perhaps I am missing something obvious...

André


More information about the Tutor mailing list