Is there a way to write the following function so that my IDE doesn't complain that column is an unused variable?
def get_selected_index(self): (path, column) = self._tree_view.get_cursor() return path[0]
In this case I don't care about the second item in the tuple and just want to discard the reference to it when it is unpacked.
In python tuples can be unpacked using a function in function tuple is passed and in function values are unpacked into normal variable.
Unpacking Tuples When we put tuples on both sides of an assignment operator, a tuple unpacking operation takes place. The values on the right are assigned to the variables on the left according to their relative position in each tuple . As you can see in the above example, a will be 1 , b will be 2 , and c will be 3 .
We can do the tuple unpacking right inside the for loop itself because anything you can put on the left-hand side of the equal sign, you can put in between the for and the in in a for loop.
Because parentheses of tuples can be omitted, multiple values can be assigned to multiple variables in one line as follows. An error is raised if the number of variables does not match the number of elements.
In Python the _
is often used as an ignored placeholder.
(path, _) = self._treeView.get_cursor()
You could also avoid unpacking as a tuple is indexable.
def get_selected_index(self): return self._treeView.get_cursor()[0][0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With