-1

I have trouble with understanding the role of the underscore in last line of below code. I've read that functions of _ consists of storing last expression, but can be used as well to omit certain values. I don't now why it is used here. Thank You in advance for help

I found this thread, which was helpful with understanding of functions which _ can fulfill, but I still don't know in which way it was used in this code: What is the purpose of the single underscore "_" variable in Python?

@staticmethod  
def frames():  
  camera = cv2.VideoCapture(Camera.video_source)  
  if not camera.isOpened():  
    raise RuntimeError('Could not start camera.')  
  while True:  
  # read current frame  
    _, img = camera.read() #Obtain images captured by the camera
Paweł Magdański
  • 189
  • 2
  • 3
  • 11
  • 4
    It is a valid identifier for variables and is used for the variables that are not used later in the code (ignorable) by convention. Basically, you can use another name (i.e., identifier) for that returned variable but if you will never use it again in the code, you should use `_`, so people who read the code will easily understand it is ignorable. – Alperen Apr 08 '21 at 20:43
  • 1
    Its another common usage is `for` loops (for the same reason): `for _ in range(5): print("hello")` will print hello five times and you don't care about what `range` returns, so you use `_`. – Alperen Apr 08 '21 at 20:54
  • 1
    `_` means *"pile of junk"*. – Mark Setchell Apr 08 '21 at 20:54
  • 1
    As Alperen said its for variables you do not need to use later in code, but I would still only use it in such cases where picking a particular name does not have any added utility. Sometiems even if the use of an identifier is short lived, giving it the right name can help document the code. When an identifier is not long lived and when it a name does not help...then using _ to keep the code compact seems a good practice. – Andrew Allaire Apr 08 '21 at 20:59

3 Answers3

3

It's literally just a variable that you won't use, so we just add an underscore. because it is technically a valid variable name. In your code, all you want from the _, img = camera.read() #Obtain images captured by the camera line is the image, so you just assign whatever the other returned value is to the null variable _. Alternatively, you could do img = camera.read()[0]

Dugbug
  • 454
  • 2
  • 11
1

As mentioned elsewhere it's a convention, but more specifically "_" typically absorbs a return value from a function or statement that you don't intend to use anywhere.

Jamie Deith
  • 706
  • 2
  • 4
-2

This is a coding convention in Python, that is to let others know the variable is meant to be private. There is no actual way to make a variable private in Python, as in other languages such as Java and C#.

https://www.geeksforgeeks.org/private-variables-python/

  • 2
    The Python convention for the private variables is using an underscore before the variable name. For example, `_some_string = "this will be private"`. Single underscore means that the variable is not used in the code. So, `_` is like a placeholder. – Alperen Apr 08 '21 at 20:50