Is there a better way to write this code to alter an object inheriting from `list` in python?

StackOverflow https://stackoverflow.com/questions/21839010

  •  12-10-2022
  •  | 
  •  

Question

In response to a previous question I had, I am now wondering it there is any way of making the following code look nicer:

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:])

Any ideas as to how I can remove the super(Cycle, self).__init__(...)?

Was it helpful?

Solution

 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top