In Python lists, there are two alternative ways of performing a list sort:
- Using its
.sortmethod, which sorts in place - Using the
sorted()built-in function, which returns a new sorted list
Now, if I create a custom container type (say by subclassing from itertools‘s ABC), can I do the same?
I can figure out how to make the custom container object sorts in place. But what I want is for the built-in sort() to return a new instance of that custom container object (instead of a list) when I use it.
Is this possible? If so, how?
What about just subclassing the builtin
list, and just redefiningsortmethod ?EDIT: after the OP’s comment, leaving
sortmethod as is and adding a new one:The
sortmethod inherited from parent remains as is (in place), the newsort_copymethod creates a new one.