有没有更好的方法来编写这段代码来改变从python中继承`list`的对象?

时间:2014-02-17 20:50:05

标签: python inheritance super

为了回应我的a previous question,我现在想知道有什么方法可以使下面的代码看起来更好:

class Cycle(list):

    def insertextend(self, index, other, reverse = False):
        """Extend the cycle by inserting an iterable at the given position."""
        index %= len(self)
        if reverse:
            super(Cycle, self).__init__(self[:index] + list(reversed(other)) + self[index:])
        else:
            super(Cycle, self).__init__(self[:index] + other + self[index:])

关于如何移除super(Cycle, self).__init__(...)

的任何想法

1 个答案:

答案 0 :(得分:0)

 def insertextend(self, index, other, reverse = False):
            """Extend the cycle by inserting an iterable at the given position."""
            index %= len(self)
            if reverse: other = reversed(other)
            self[index:index] = other