473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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

>>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?
Feb 6 '07 #1
0 1026

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Hilde Roth | last post by:
This may have been asked before but I can't find it. If I have a rectangular list of lists, say, l = ,,], is there a handy syntax for retrieving the ith item of every sublist? I know about for i...
2
by: Robert Brewer | last post by:
>>> class A(types.DictType): .... def __getitem__(self, key): .... return types.DictType.__getitem__(self, key) .... >>> a = A() >>> a = 3 >>> a.__getitem__ = lambda x: "w00t" >>>...
34
by: jblazi | last post by:
Let us assume I have a list like and would like to transoform it into the string '{1,2},{7,8},{12,13}' Which is the simplest way of achiebing this? (The list is in fact much longer and...
8
by: Steven Bethard | last post by:
Sorry if this is a repost -- it didn't appear for me the first time. So I was looking at the Language Reference's discussion about emulating container types, and nowhere in it does it mention...
21
by: ron | last post by:
Why doesn't this work? >>> def foo(lst): .... class baz(object): .... def __getitem__(cls, idx): return cls.lst .... __getitem__=classmethod(__getitem__) .... baz.lst = lst .... ...
54
by: seberino | last post by:
Many people I know ask why Python does slicing the way it does..... Can anyone /please/ give me a good defense/justification??? I'm referring to why mystring gives me elements 0, 1, 2 and 3...
4
by: spike | last post by:
I've googled like crazy and can't seem to find an answer to why this isn't working. I want to create a custom list class that acts as a circular list. ie: my_list = (0, 1, 2) how I want it...
11
by: JJLaRocque | last post by:
Hi all, Is there a simple python function to return the list index of the minimum entry in a list of lists? ie, for , , ] to return 2,4. Or, same question but just for a list of numbers, not a...
2
by: Kirk Strauser | last post by:
I want to subclass list so that each value in it is calculated at call time. I had initially thought I could do that by defining my own __getitem__, but 1) apparently that's deprecated (although I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.