Subclassing list and splicing

Kreso kkNOWAYumer at donoevil.com
Thu Sep 3 13:10:12 EDT 2009


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:


class mylist(list):

    def __init__(self):
        list.__init__(self)


k = mylist()
k.append(1)
k.append(2)
k.append(3)
print type(k)  # prints <class '__main__.mylist'> as expected
m = k[:1]
print type(m)  # prints <type 'list'>  ???


I would prefer that resulting object m belonged to myclist class.
How to obtain such behaviour? Must I somehow implement __getslice__
method myself?

(It's python 2.5.2, BTW)



More information about the Python-list mailing list