Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why does list.__getitem__ return a list instance for subclasses ofthe list type?

Reply
Thread Tools

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

 
 
dackz
Guest
Posts: n/a
 
      02-06-2007
>>> class ListyThing(list): pass
....
>>> assert isinstance(ListyThing()[:], ListyThing) # I expect True!

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>> 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:

>>> class DictyThing(dict): pass

....
>>> assert isinstance(DictyThing().copy(), DictyThing) # I expect True!

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>> type(DictyThing().copy()) # I expect DictyThing!

<type 'dict'>

Any thoughts?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
how to delete the close button (the X on the right most corner ofthe window) on a window Jeremy Bowers Python 3 03-14-2005 05:05 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Is it possible to have instance variables in subclasses of builtins? Kenneth McDonald Python 2 06-16-2004 01:02 PM
REALLY CHEAP computer programming ebook libraries just follow any ofthe links below =?ISO-8859-1?Q?=A0?= Java 0 01-10-2004 01:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57