0

I am not an experienced Python programmer and I saw following code which I couldn't understand. Unfortunately syntax is very tricky and difficult to search for on the internet. Though I did find some explanation to '_' and '__' but I am not sure if following code has any special meaning for '_'

if not allowed_positions:
    return (0, 0)
_, point = max([(self.point(graph.find_point(p), self), p) for p in allowed_positions])

In the above code I don't understand why there is an underscore with comma '-,' before point = ....

Adam Smith
  • 45,072
  • 8
  • 62
  • 94
Vijayendra
  • 1,793
  • 2
  • 16
  • 20
  • 3
    I'm confused about your title, though, since there is no for loop in this code. – Adam Smith May 14 '17 at 17:22
  • @AdamSmith There is a for loop -> "for p in allowed_positions" which creates an array and then max function finds max value from this array. But what confused me was "_," before point. I was this syntax couple of times with loops. – Vijayendra May 14 '17 at 17:45
  • @DevBoy that's called a list comprehension. It's a totally different syntactical construct. `[expression for item in iterable]` – Adam Smith May 14 '17 at 17:50
  • @DevBoy From the title, I was expecting the question `for _ in range(5): do_something()` or `acc = 0; for _ in some_list: acc += 1` or etc. – Adam Smith May 14 '17 at 17:52

2 Answers2

4

_ is just used as a placeholder for a discarded variable. Let's assume there is a function which returns a tuple with two elements, and I am interested only in the second part of the tuple, then it is a general practice to use _ for the variable I do not need. e.g.

>>> def return_tuple():
...     return (24,7)
... 
>>> _, days = return_tuple()
>>> days
7
DhruvPathak
  • 38,316
  • 14
  • 103
  • 164
  • My edit shortly after your edit (which is not shown in the history) killed your modifications. Feel free to restore your original answer if you are not satisfied with the current state of the post (I think it's fine). – timgeb May 14 '17 at 17:25
  • @timgeb Done. Thanks. – DhruvPathak May 14 '17 at 17:26
  • Thanks @DhruvPathak for short and clear explanation. – Vijayendra May 14 '17 at 17:52
  • 1
    Might be worth adding explicitly that `_` has no special meaning to the language, it's only used by convention (unlike some languages like Haskell and Go where `_` is a language construct that throws away the result) – Adam Smith May 14 '17 at 17:53
  • 1
    @AdamSmith: in `Python` prompt `_` stores last executed unassigned operation result. E. g.: after executing `max(range(10))` `_` name will be assigned to value `9` – Azat Ibrakov May 14 '17 at 18:16
  • 1
    @AzatIbrakov that's a feature of the interactive repl, not of the language. Though I had forgotten that was the case! :) Regardless it isn't what is being asked here, is it? – Adam Smith May 14 '17 at 18:18
1

_ is a placeholder for variables that you don't need to store data in.

You can use it for tuple unpacking and it's common to practice to use an underscore to denote that that value will not be used later in the script.

If you had something like this: soldiers = [('Steve', 'Miller'), ('Stacy', 'Markov'), ('Sonya', 'Matthews'), ('Sally', 'Mako')]

and, you wanted to get only the last names you would do this:

for _, last_name in soldiers:
    # print the second element
    print(last_name)

Instead of doing:

for first_name, last_name in soldiers:
    print(last_name

Since you don't need to use first_name. You replace it with _ so you don't store unnecessary variables

Noah Cristino
  • 763
  • 8
  • 29
Garrett Kadillak
  • 912
  • 7
  • 17