list inheritance or delegation?

Robert Brewer fumanchu at
Tue Mar 16 18:51:43 CET 2004

George Young wrote:
> I need an object that behaves much like a list, but has some
> additional features.  It seems that I can do this either by
> inheriting from type list, or by emulating list by defining
> __len__, __getitem__, and maybe a bunch of  other methods.  Thirdly,
> I could inherit from UserList, but that's deprecated now.
> Inheriting from list would save me implementing a bunch of methods
> that I might want.  But if I inherit from list, how do I get access
> to the data?  E.g., I need a customized 'setitem' method that does
> the insert and then does some other things too.  With 
> UserList, I could:
> class mylist(UserList):
>    def __setitem__(self,i,y):
> 	self.dostuff(y)
> but when inheriting from 'list', I'm not sure how to do this.

I'll let others answer the deeper questions. For now, just wanted to
recommend you call the superclass to handle your __setitem__ example
(subclassing from list):

class mylist(list):
    def __setitem__(self, key, value):
        list.__setitem__(self, key, value)


Robert Brewer
Amor Ministries
fumanchu at

More information about the Python-list mailing list