Subclassing list and splicing

Emile van Sebille emile at fenx.com
Thu Sep 3 13:37:39 EDT 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:
<snip>
> 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:
 list.__init__(self)
 else:
 list.__init__(self,val)
 def __getslice__(self,slstart,slend,slstep=None):
 return mylist(self[slstart:slend:slstep])
Emile


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /