Skip to content Skip to sidebar Skip to footer

Single Class Python List

How can every set method in python's list object be overridden in a derived object such that every item in that list is of a specific class? Consider class Index(int): pass c

Solution 1:

You'll have to implement the various directly; the underlying C implementation does not call __setitem__ for each and every change, as it is far more efficient to directly manipulate the (dynamically grown) C array.

Take a look at the collections abstract base classes, specifically at the MutableSequence ABC, to get an idea of what methods all can mutate your list, to maintain your type invariant you'd need to implement insert, append, extend and __iadd__.

Better still, you can use the collections.MutableSequence() class as an alternative base class to list; this is a pure-python implementation that does cast many of those methods as calls to a core set of methods; you'd only need to provide implementations for __len__, __getitem__, __setitem__, __delitem__ and insert; any method named in the Abstract Methods column of the table.

classIndexList(collections.MutableSequence):
    def__init__(self, int_list):
        self._list = []
        for el in int_list:
            self.append(Index(el))

    def__len__(self): returnlen(self._list)
    def__getitem__(self, item): return self._list[item]
    def__delitem__(self, item): del self._list[item]

    def__setitem__(self, index, value):
        self._list.key[index] = Index(value)

    definsert(self, index, value):
        self._list.insert(index, Index(value))

Post a Comment for "Single Class Python List"