[Tutor] custom container subclassing list-- slices are lists!?

John Fouhy john at fouhy.net
Mon Aug 21 23:07:52 CEST 2006

On 22/08/06, Marcus Goldfish <magoldfish at gmail.com> wrote:
> >>> class MyFoo(list):
>            def __init__(self, inlist):
>                   self = inlist[:]

Um, I'm fairly sure this line is not doing what you think it is doing!

self is just a local variable.  When __init__ is called, self is bound
to the MyFoo instance.  But when you do 'self = inlist[:]', you just
rebind it to a copy of inlist.  You aren't changing self..

> >>> me = MyFoo(range(10))
> >>> type(me)
> <class '__main__.MyFoo'>
> >>> type(me[0:3])
> <type 'list'>

Have you tried implementing __getitem__ and __setitem__ for slice objects?
(see http://docs.python.org/ref/sequence-methods.html and


More information about the Tutor mailing list