This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: array.array is not an instance of collections.MutableSequence
Type: enhancement Stage: resolved
Components: Versions: Python 3.5
process
Status: closed Resolution: duplicate
Dependencies: Superseder: collections.abc.Reversible doesn't fully support the reversing protocol
View: 29727
Assigned To: Nosy List: Alexander Gosselin, Jim Fasarakis-Hilliard, cheryl.sabella
Priority: normal Keywords:

Created on 2017-04-21 15:00 by Alexander Gosselin, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg292053 - (view) Author: Alexander Gosselin (Alexander Gosselin) Date: 2017-04-21 15:00
array.array has all of the methods required by collections.MutableSequence, but:

>>> import array
>>> import collections
>>> isinstance(array.array, collections.MutableSequence)
False
msg292063 - (view) Author: Jim Fasarakis-Hilliard (Jim Fasarakis-Hilliard) * Date: 2017-04-21 16:33
Thanks for the bug report, Alexander. This is related, if not a superseder, of: https://bugs.python.org/issue29727
msg292069 - (view) Author: Alexander Gosselin (Alexander Gosselin) Date: 2017-04-21 17:35
Thanks for taking a look at this. I think array is little used, so registering it as a member of some of the abstract base classes in collections could easily have been overlooked.

One of the prerequisites for membership in MutableSequence is a .clear() method, and array doesn't have one, though there is no reason why it shouldn't. Here's the workaround that I'm using:

import array
import collections

class array(array.array,
            collections.MutableSequence):
    __slots__ = ()
    
    def __reversed__(self):
        # this is a bit hacky
        return reversed(self.tolist())
        
    def clear(self):
        self.__setitem__(slice(None), self.__class__(self.typecode))
        
Here are some results:

>>> a = array('f', [1,2,3,4])
>>> list(reversed(a))
[4.0, 3.0, 2.0, 1.0]
>>> a.clear()
>>> a
array('f')
msg335019 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2019-02-07 13:09
As Jim mentioned, issue 29727 also discusses registering array.array.

See issue 23864, issue 25737, issue 35190, and issue 35349 for discussions about collections.abc in general.

Closing this as a duplicate.
History
Date User Action Args
2022-04-11 14:58:45adminsetgithub: 74316
2019-02-07 13:09:21cheryl.sabellasetstatus: open -> closed

superseder: collections.abc.Reversible doesn't fully support the reversing protocol

nosy: + cheryl.sabella
messages: + msg335019
resolution: duplicate
stage: resolved
2017-04-21 17:35:20Alexander Gosselinsetmessages: + msg292069
2017-04-21 16:33:23Jim Fasarakis-Hilliardsetnosy: + Jim Fasarakis-Hilliard
messages: + msg292063
2017-04-21 15:00:36Alexander Gosselincreate