How to append to a custom python Sequence?

I'm developing an engine with Python and Pygame, and I've implemented a custom Sequence class called "Animation", where each index is a frame (In that case, another object of mine, a "grict/grid-element", which holds its own Pygame.Surface object). Here's my overloaded implementation concerning the "overloaded part":

class Animation(Sequence):

    """
    A Python animation object is a sequence of Pygame surfaces, where each surface represents a frame.
    """

    LOOP_FORWARDS = True
    LOOP_BACKWARDS = False

    def __init__(self, grid, *frames):

        self.grid = grid
        self.frame = 0
        self.step = 1
        self.direction = Animation.LOOP_FORWARDS
        self.frames = frames

    def __getitem__(self, frame):

        return self.frames[frame]

    def __len__(self):

        return len(self.frames)

If I create an instance of the Animation class, I can access each frame with brackets[], and that's beautiful. It happens that I want to implement an "addItem" method, or use an ".append" to the instance, and I don't know how to do it.

def addFrame(self, frame):

        """
        Appends a new frame to the animation's inner frame-collection.
        """

        self.frames.__append__(frame) # There's no such thing, and, if there was, I suppose I could access the .append() method directly from the instance.

Answers


When you use *frames form, it would be a tuple. You can confirm this with

print type(frames)

So, you might want to convert that to a list like this

self.frames = list(frames)

and then use the list.append function like this

self.frames.append(frame)

Need Your Help

How to get rid of the console window

c++ windows console mingw

I have tried to make a simple MessageBox using this code: