8000 Preserve type on List.copy() and slicing · Issue #149 · vberlier/nbtlib · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Preserve type on List.copy() and slicing #149
Open
@MestreLion

Description

@MestreLion

I just noticed List.copy() returns a plain list, 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., should List.__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 write b = List[xxx](a.copy()) to preserve type and easy to do b = list(a.copy()) to guarantee a plain list.

  • In a general way, should the mutables Compound and List 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 in collections.abc.MutableSequence.

Meanwhile, what's your opinion on this? Interested in a PR to implement copy() and related methods in Compound and List?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0