单类python列表

单类python列表,python,overriding,Python,Overriding,如何在派生对象中重写python列表对象中的每个set方法,使该列表中的每个项都属于特定的类 考虑 class Index(int): pass class IndexList(list): def __init__(self, int_list): for el in int_list: self.append(Index(el)) def __setitem__(self, key, val): supe

如何在派生对象中重写python列表对象中的每个set方法,使该列表中的每个项都属于特定的类

考虑

class Index(int):
    pass


class IndexList(list):

    def __init__(self, int_list):
        for el in int_list:
            self.append(Index(el))

    def __setitem__(self, key, val):
        super(IndexList, self).__setitem__(key, Index(val))

    # override append insert etc...
这是否可以在不直接重写向列表中添加元素的每个函数的情况下完成?我以为简单地重写
\uuuuuu setitem\uuuuuu
就足够了

例如,如果未覆盖
append

ilist = IndexList([1,2])
ilist.append(3)

for i in ilist:
    print(isinstance(i, Index)) # True, True, False

您必须直接实现各种各样的功能;底层C实现不会为每个更改调用
\uuuuu setitem\uuuu
,因为直接操作(动态增长的)C数组要高效得多

查看一下,特别是
MutableSequence
ABC,了解所有方法都可以改变列表,为了保持类型不变,您需要实现
insert
append
extend
\iadd

更好的是,您可以使用
collections.MutableSequence()
类作为
list
的替代基类;这是一个纯python实现,它将许多方法转换为对一组核心方法的调用;您只需要提供
\uuu len\uuuuuuuu
\uuuu getitem\uuuuuuuu
\uuuuuuu setitem\uuuuuuu
insert
的实现;表的“抽象方法”列中命名的任何方法

class IndexList(collections.MutableSequence):
    def __init__(self, int_list):
        self._list = []
        for el in int_list:
            self.append(Index(el))

    def __len__(self): return len(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)

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

您必须直接实现各种各样的功能;底层C实现不会为每个更改调用
\uuuuu setitem\uuuu
,因为直接操作(动态增长的)C数组要高效得多

查看一下,特别是
MutableSequence
ABC,了解所有方法都可以改变列表,为了保持类型不变,您需要实现
insert
append
extend
\iadd

更好的是,您可以使用
collections.MutableSequence()
类作为
list
的替代基类;这是一个纯python实现,它将许多方法转换为对一组核心方法的调用;您只需要提供
\uuu len\uuuuuuuu
\uuuu getitem\uuuuuuuu
\uuuuuuu setitem\uuuuuuu
insert
的实现;表的“抽象方法”列中命名的任何方法

class IndexList(collections.MutableSequence):
    def __init__(self, int_list):
        self._list = []
        for el in int_list:
            self.append(Index(el))

    def __len__(self): return len(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)

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

您必须直接实现各种各样的功能;底层C实现不会为每个更改调用
\uuuuu setitem\uuuu
,因为直接操作(动态增长的)C数组要高效得多

查看一下,特别是
MutableSequence
ABC,了解所有方法都可以改变列表,为了保持类型不变,您需要实现
insert
append
extend
\iadd

更好的是,您可以使用
collections.MutableSequence()
类作为
list
的替代基类;这是一个纯python实现,它将许多方法转换为对一组核心方法的调用;您只需要提供
\uuu len\uuuuuuuu
\uuuu getitem\uuuuuuuu
\uuuuuuu setitem\uuuuuuu
insert
的实现;表的“抽象方法”列中命名的任何方法

class IndexList(collections.MutableSequence):
    def __init__(self, int_list):
        self._list = []
        for el in int_list:
            self.append(Index(el))

    def __len__(self): return len(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)

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

您必须直接实现各种各样的功能;底层C实现不会为每个更改调用
\uuuuu setitem\uuuu
,因为直接操作(动态增长的)C数组要高效得多

查看一下,特别是
MutableSequence
ABC,了解所有方法都可以改变列表,为了保持类型不变,您需要实现
insert
append
extend
\iadd

更好的是,您可以使用
collections.MutableSequence()
类作为
list
的替代基类;这是一个纯python实现,它将许多方法转换为对一组核心方法的调用;您只需要提供
\uuu len\uuuuuuuu
\uuuu getitem\uuuuuuuu
\uuuuuuu setitem\uuuuuuu
insert
的实现;表的“抽象方法”列中命名的任何方法

class IndexList(collections.MutableSequence):
    def __init__(self, int_list):
        self._list = []
        for el in int_list:
            self.append(Index(el))

    def __len__(self): return len(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)

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

我想不出一个好的用例来实现这个。。。请你开导我;)我还在争论我的意图是否有用。我计划得越多,我就越觉得它不是。我想不出一个好的用例来解决这个问题。。。请你开导我;)我还在争论我的意图是否有用。我计划得越多,我就越觉得它不是。我想不出一个好的用例来解决这个问题。。。请你开导我;)我还在争论我的意图是否有用。我计划得越多,我就越觉得它不是。我想不出一个好的用例来解决这个问题。。。请你开导我;)我还在争论我的意图是否有用。我计划得越多,就越觉得不是。