0

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
bad_coder
  • 73
  • 1
  • 6
  • I'm wondering when you will need it. if you create variable to keep `Rect` then it should always keep `Rect` - don't mess it. And if you want to keep different objects on list - ie. Player, Enemy, etc. then all of them should have exactly the same methods - `draw`, `update`, etc. instead of `draw_player`, `draw_enemy`, `update_player`, `update_enemy` - and then you don't have to check class to decide if you have to use `draw_player` or `draw_enemy` - you always has to use `draw`. – furas Jan 03 '21 at 21:22
  • 1
    Oh I have mine for functions. I have an optional argument for centering, and I want it to automatically center stuff if a rect is passed. – bad_coder Jan 03 '21 at 23:53

1 Answers1

1

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')
Rabbid76
  • 202,892
  • 27
  • 131
  • 174