Doubt regarding Python syntax

1

Good evening, my question is about a piece of code I found in a book about Python and OpenCV. The author created the function that I will put below:

@property
def frame(self):
    if self._enteredFrame and self._frame is None:
        _, self._frame =  self._capture.retrieve()
    return self._frame

My question is regarding the line: _, self._frame = self._capture.retrieve (). Why does an underline and a comma before self._frame = ...?

    
asked by anonymous 14.10.2018 / 05:10

1 answer

2

This is a de-structurer. self._capture.retrieve () returns a tuple with 2 elements, let's assume 'apple' and 'orange' With the following syntax:

frutas = ('maçã', 'laranja')

You would have a tuple with apple in position 0, and orange in position 1, but you can also pick up the values in this way:

fruta1, fruta2 = ('maçã', 'laranja')

In this case fruit1 will have apple, and fruit2 will have orange.

If you do not want to use the first value returned, it is recommended that you use a _ to capture this value, as is the case with your code, that is, _ contains the first value of the tuple, which is irrelevant, and self ._frame contains the second value of the tuple.

    
14.10.2018 / 05:32