39

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.

Nathan
  • 5,272
  • 2
  • 26
  • 28

4 Answers4

56

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]
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • 24
    It's worth noting that `_` is not any kind of syntactic magic, but a convention (of course, it's a convention recognised by IDEs and pylint, and Python conventions are pretty strong...) – detly Sep 27 '10 at 09:18
  • 2
    Python would be nothing without conventions! – fredley Sep 27 '10 at 09:19
  • 4
    Is this convention documented? – Nathan Sep 27 '10 at 09:20
  • @fredley - I'm not disputing that at all :) but still worth mentioning. @Nathan - ...maybe? – detly Sep 27 '10 at 09:46
  • The same is often used in Lua; I suspect it's a common theme in languages that support this sort of unpacking. – Glenn Maynard Sep 27 '10 at 10:22
  • 1
    It is common in OCaml and Haskell. – Gaius Sep 27 '10 at 13:58
  • And is also common in Prolog: in fact it is a little more than a convention, it is an anonymous variable (and can be repeated, without it meaning the same value) in a matching statement. IIRC Erlang handles it similarly. – Matthew Schinckel Sep 28 '10 at 00:17
  • 8
    @Gaius: I believe in Haskell it explicitly means "discard this value"; in Python `_` is still an assigned variable that you could in theory use just like any other variable. – me_and Sep 30 '10 at 12:52
  • 2
    The python command line interpreter "magically" assigns _ to the result of the last computation so if you run something but forget to store the return value, it will be captured for you in the `_` variable. It's a useful thing while doing interactive work. In the above case, I'd probably do indexing rather than unpacking though. – Noufal Ibrahim Oct 19 '10 at 10:40
  • The parentheses in the first code snippet are unnecessary, aren't they? – matt b Jul 22 '13 at 15:03
  • @mattb: They are optional. – kennytm Jul 22 '13 at 18:05
  • What if your tuple has three or more elements? I do this all the time, especially in test code that creates dummy objects. – Michael Wolf Feb 12 '23 at 15:28
4

If you don't care about the second item, why not just extract the first one:

def get_selected_index(self):
    path = self._treeView.get_cursor()[0]
    return path[0]
Steven
  • 28,002
  • 5
  • 61
  • 51
  • Note that the return should now be `return path`, not `return path[0]` – Michael Mior Sep 27 '10 at 14:59
  • 3
    This is weaker, because it removes the assertion that there are exactly two items. – Josh Lee Sep 28 '10 at 00:39
  • @Michael Mior: I've rolled back your edit: Based on the original question, it should still be `path[0]` (`path` seems to be a sequence itself) – Steven Sep 28 '10 at 13:40
  • 1
    I'd argue that this is less explicit as the _ placeholder convention. There's potential ambiguity in the order of operations, is it: `(path = self._treeView.get_cursor())[0]` or `path = (self._treeView.get_cursor()[0])` – Adam Parkin Jan 31 '12 at 00:11
1

Yes, it is possible. The accepted answer with _ convention still unpacks, just to a placeholder variable.

You can avoid this via itertools.islice:

from itertools import islice

values = (i for i in range(2))

res = next(islice(values, 1, None))  # 1

This will give the same res as below:

_, res = values

The solution, as demonstrated above, works when values is an iterable that is not an indexable collection such as list or tuple.

jpp
  • 159,742
  • 34
  • 281
  • 339
0

it looks pretty, I don't know if a good performance.

a = (1, 2, 3, 4, 5)
x, y = a[0:2]
east
  • 21
  • 1