Subclassing list and splicing

Emile van Sebille emile at
Thu Sep 3 19:37:39 CEST 2009

On 9/3/2009 10:10 AM Kreso said...
> I am subclassing list class and it basically works, but I don't
> understand why after splicing these mylist objects I don't get
> returned mylist objects. What I get are list objects:
> I would prefer that resulting object m belonged to myclist class.
> How to obtain such behaviour? Must I somehow implement __getslice__
> method myself?

Yep -- something like:

class mylist(list):
     def __init__(self,val=None):
         if val is None:
     def __getslice__(self,slstart,slend,slstep=None):
         return mylist(self[slstart:slend:slstep])


More information about the Python-list mailing list