-
-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preserve type on List.copy() and slicing #149
Comments
Some research indicates slicing (and by consequence
Given that, now I have a strong opinion that at least |
Yeah this can't be easily handled right now because lists and compounds inherit directly from the builtins instead of |
I was planning on overriding each method anyway, no problem with that. You're already overriding Yeah, ABCs used to be the norm, but there are legitimate cases for subclassing the builtins direcly. It used to be a chore and have a million caveats, but not anymore, specially in Python 3 (and even more with Protocols, etc). The builtins do inherit from the ABCs, so you can already use |
Hmmm, now I'm also wondering about other tags like numeric and strings. Should string slicing or methods like |
Hum, very interesting point! Yeah, I believe they should preserve type too. Both |
I just noticed
List.copy()
returns a plainlist
, as it doesn't override the copy method, and was about to create a PR implementing it. Then I realized this is a deeper issue, so I need your opinion before moving on:list.copy() == list[:]
, so copy if was changed to preserve type, should slicing be changed too? I.e., shouldList.__getitem__
use the a similar logic as__setitem__
, and do something like this:if isinstance(index, slice): return self.__class__(super().__getitem__(index))
.It would be surprising if
copy()
preserve type but slicing didn't. In one hand, some might expect (and rely on) copy and slicing to return a plain list. OTOH, it's awkward to writeb = List[xxx](a.copy())
to preserve type and easy to dob = list(a.copy())
to guarantee a plain list.In a general way, should the mutables
Compound
andList
preserve type in slicing and subsets?I could do some research to see what
numpy.ndarray
and other similar list subclasses do, and also if there's any principle suggested incollections.abc.MutableSequence
.Meanwhile, what's your opinion on this? Interested in a PR to implement
copy()
and related methods in Compound and List?The text was updated successfully, but these errors were encountered: