Search code examples
pythonpygame-surfacemagic-function

Returning value when instance "called"


I want a certain function callable on a class. Something similar to:

class foo():
    def __init__(self):
        self.img = pygame.Surface((20, 20))
    def magicfunction(self):
        return self.img

bar = foo()

screen = pygame.display.set_mode((200, 200))
screen.blit(bar)

Which magic-function do I have to use?


Solution

  • If I understand you correctly, you want to create a class of your own, which is also a surface. That sounds exactly like inheritance! Try making foo a child of pygame.Surface:

    class foo(pygame.Surface):
        def __init__(self):
            pygame.Surface.__init__(self, (20, 20))
            more_data = "You should be able to extend this class freely"