1

Can someone please explain this Python code to me?

import pygame

click, _, _ = pygame.mouse.get_pressed()

I know that pygame.mouse.get_pressed() returns an array indicating if the mouse is clicked. But there are 3 output objects in the code: First is click, second is _? third is _?

Why do the second and third output objects have the same name and is an underscore? What do they represent? Thanks a lot!

user3840170
  • 11,878
  • 11
  • 37
user196736
  • 51
  • 4
  • 2
    An underscore is used in place of a "real" variable to indicate a variable that isn't needed but is still returned. The code will set the variable `click` to the first returned argument, and set `_` (which by convention shouldn't be used) to the third variable (and the second too but the last allocation is `_ = (...)[2]`). This is a general python thing and isn't related to pygame. – Rotem Shalev Mar 30 '21 at 09:28
  • This is not a pygame question. It is a very basic python question. – Rabbid76 Mar 30 '21 at 09:30
  • 2
    Does this answer your question? [Reason for Assignment to " \_ "](https://stackoverflow.com/questions/4859909/reason-for-assignment-to) – Tomerikoo Mar 31 '21 at 14:19

1 Answers1

0

first _ and second _ just represent elements in array which we don't want get

code example:

array = [1, 2, 3]

val1, _, _ =  array  # initialize and value assignment to val1
print(val1)          # output: 1
Mustafa Aydın
  • 6,383
  • 3
  • 8
  • 25
Max
  • 9
  • 1