1

EDIT: could it be that this is just an oversight that has not been addressed? The standard types documentation includes .copy() and .clear() in the table of methods for mutable sequence types.

I noticed something a little curious this morning: the list object in Python >3.3 includes a .copy() method, and also a .clear() method. However the collections.abc Sequence and MutableSequence abstract base classes do not include these methods are part of their spec (.clear() would only make sense as part of the MS spec, of course).

>>> x = [1,2,3]
>>> x.copy()
[1, 2, 3]
>>> x.clear()
>>> x
[]

My understanding is that part of the reason to use MutableSequence is to signal to the universe you want your object to "act like a list" (unless clearly stated otherwise):

from typing import Sequence

class S(Sequence):
    x = [1, 2, 3]
    def __getitem__(self, item):
        return self.x[item]
    def __len__(self):
        return len(self.x)

Yet a fully defined S type cannot be copied (and a MS cannot be cleared) like a list:

>>> s = S()
>>> s.copy()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'S' object has no attribute 'copy'

This seems weird and as with most things like this, there's probably a good reason for it. What is it?

Rick supports Monica
  • 33,838
  • 9
  • 54
  • 100

1 Answers1

1

Found an answer- at least with regards to copy()- after posting the question: it seems this issue was discussed quite a while ago in the bug tracker. A couple quotes:

I don't think this is needed nor do I think that it is a good idea to have a copy() method in the ABCs because they know so little about their concrete underlying class (perhaps the backing store in the filesystem or a database).

and:

The return type of copy() for ABCs feels problematic. MutableMapping doesn't have it either.

As I expected, this was discussed and there seem to be sound reasons behind it.

This little tidbit from GVR is also worth including:

I personally despise almost all uses of "copying" (including the entire copy module, both deep and shallow copy functionality).

Rick supports Monica
  • 33,838
  • 9
  • 54
  • 100
  • That discussion isn't about lists/mutable sequences: it's title is "collections.**abc.Set** doesn't provide copy() method" (emphasis mine). – martineau Jul 02 '18 at 15:21
  • @martineau Yeah but if you read the discussion they talk about sequences, too. – Rick supports Monica Jul 02 '18 at 15:21
  • 1
    @martineau or i should say: it turns into a general dicussion about all ABCs, at least with regards to `copy()`. doesn't explain why `clear()` is left out though. – Rick supports Monica Jul 02 '18 at 15:23
  • Regardless of why, couldn't you just derive or even create your own ABC with the desired methods? – martineau Jul 02 '18 at 15:24
  • @martineau yup, you certainly could. but it's nice when the object model "makes sense" on its own and you don't have to remember to use the special ABC you created on your own. but mostly i was just wondering why this was. – Rick supports Monica Jul 02 '18 at 15:26
  • 1
    Obviously its not an oversight. The "tidbit" you added mentioning the very end of the discussion by Guido is all the answer you'll ever get (since he's the one who closed the issue): **Status: closed, Resolution: rejected**. – martineau Jul 02 '18 at 15:31