Most .copy() methods should be classmethods.

Issue #280 resolved
Anonymous created an issue

I did this using pygame.Rect, but it appears to be the same for most things.

If I have a code like so:

class MyRect(pygame.Rect):
    color = (255, 255, 255)

    def draw(self, screen):
        screen.fill(self.color, self)

r1 = MyRect()
r2 = r1.copy()

It will raise an error:

AttributeError: 'pygame.Rect' object has no attribute 'draw'

If .copy() was a classmethod instead, it would create a copy of MyRect instead. Now I have to manually override .copy() everytime.

Comments (3)

  1. Lenard Lindstrom

    This is a common quirk of many Python extension types. It is not fixed by making .copy() a classmethod. If updating .copy() to return the type of the copied object does not break existing Pygame code then this feature can be considered for 1.9.2. Otherwise it will be held over to the SDL 2.0 version of Pygame.

  2. Lenard Lindstrom

    For Surface, Rect, and Color types, resolve Issue #280

    For these types, methods or operations that returned a new instance always return an instance of the base type. For instance, Surface.copy() always returned a new Surface, event for a subclass of Surface. This changeset makes Pygame more subclass friendly by altering Surface.copy() and the like to return a new instance of class of the object on which it is called.

    Surface methods changed to work better on subclasses are copy(), convert(), convert_alpha(), and subsurface(). For Rect the copy(), move(), inflate(), clamp(), clip(), union(), union_all(), and fit() methods are updated. For the Color type the correct_gamma() method and +, -. *. //, %, and ~ operators are upgraded.

    → <<cset ba6678fd5412>>

  3. Log in to comment