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 
2
  • 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. Commented Jan 3, 2021 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. Commented Jan 3, 2021 at 23:53

1 Answer 1

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') 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.