Search code examples
pythonvariablespygamerect

How to check if a variable contains a rect in Pygame?


So if I add a rect to a variable like so

box = pygame.Rect(x, y, w, h)

How do I check if the variable rect holds a pygame.Rect? Ideally it would return something similar to this

int = 9

#int is not a rect
#box is a rect

Solution

  • Use isinstance(object, classinfo):

    Return True if the object argument is an instance of the classinfo argument [...]

    if isinstance(box, pygame.Rect):
        print('is a rect')
    else:
        print('is not a rect')