import pygame
from pygame.sprite import Sprite
class FooObject(Sprite):
def __init__(self):
Sprite.__init__(self)
def do_stuff(self):
...doing stuff
objects = pygame.sprite.Group()
my_obj = FooObject()
objects.add(my_obj)
for obj in objects:
obj.do_stuff()
The code works fine. However, last line Pycharm gives me a warning:
Unresolved attribute reference 'do_stuff' for class 'Sprite'
Very understandable, normally i would just put a placeholder method in the super-class that gets overwritten by the inheriting class. However, the Sprite class isnt mine and i dont want to mess with it, so what is the recommended course of action here? Ignore the warning?
CodePudding user response:
You could check for existence of the method before calling:
for obj in objects:
if hasattr(obj, 'do_stuff'):
obj.do_stuff()
or
for obj in objects:
if isinstance(obj, FooObject):
obj.do_stuff()
where FooObject
could be the child class of all children having a do_stuff
member.
Edit:
From the comments it could be usefull to create an intermediate abstract class, named ActiveStripe
for grouping all descendants having a do_stuff
member.