Why does list.__getitem__ return a list instance for subclasses ofthe list type?

D

dackz

class ListyThing(list): pass
....
assert isinstance(ListyThing()[:], ListyThing) # I expect True!
Traceback (most recent call last):
File said:
type(ListyThing()[:]) # I expect ListyThing!
<type 'list'>

I don't find this intuitive. Is this intentional? I believe this could
be avoided if list.__getitem__ used "self.__class__()" to make a new
instance, instead of "list()", but I don't know how that works under
the hood in C.

I believe this happens a lot of other cases too. Actually, I wrote up
some test cases at http://brodierao.com/etc/listslice/ but I haven't
taken a look at it in quite a while. I believe there's some other
funky stuff going on there as well.

Also, this happens with dict too:
Traceback (most recent call last):
<type 'dict'>

Any thoughts?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,834
Latest member
BuyCannaLabsCBD

Latest Threads

Top