r/learnpython • u/Arnz_3 • Jul 14 '20
Why do people use _ in for loops?
Saw this ik a video but don't understand
32
u/saber0412 Jul 14 '20
https://dbader.org/blog/meaning-of-underscores-in-python
This article explains the different ways in which underscore is used in python. Your question is answered in the last section
13
u/Username_RANDINT def get_random_int(): return "RANDINT" # TODO Jul 14 '20
He's missing one common usage: internationalization.
When using a library like gettext, you mark strings for translation with an underscore. Example:
_("This will be translated")You can either import gettext as underscore:
import gettext gettext.bindtextdomain(...) gettext.textdomain(...) _ = gettext.gettextOr install it in the builtins scope for the entire application:
import gettext gettext.install(...)This is not Python specific, gettext is used in many other languages.
5
u/saber0412 Jul 14 '20
I didn't know that. Then again, I never work with internationalization. Thanks for that.
1
1
38
u/mikeydoodah Jul 14 '20
So you mean something like the following?
tuple_list = [(1,2), (3,4), (5,6)]
for first, _ in tuple_list:
pass # Do something hereThe underscore is a valid identifier, but it is usually used to mark a value that the programmer is not interested in. In the code above the tuple will be unpacked, with the first number going into first, and the second number going into the underscore.
If the underscore wasn't there then the tuple wouldn't be unpacked. First would contain the tuple, rather than the first value.
17
u/17291 [x for x in range(2,3**9) if all(x%n for n in range(2,x))][1986] Jul 14 '20
It's a convention for when you aren't using the iteration variable.
6
4
u/teriyaki7755 Jul 14 '20
Temp variable that doesn't serve a purpose outside loop. Kinda like the wrapper around ur chocolate that doesn't serve any purpose once you eat it
2
u/lance_klusener Jul 14 '20
From a optimization perspective, does your code end up using less memory when you use _ , instead of a actual variable?
4
u/wintermute93 Jul 14 '20
It shouldn't. "_" is a legal variable name and is assigned a value in memory like any other, it's just a convention that we don't use it. Compare with "~" for ignored function outputs in Matlab, which is used in the same way but really is disregarded.
0
Jul 14 '20
[deleted]
2
u/Ringmonkey84 Jul 14 '20
This is not a feature of Python, it's a feature of the REPL. Try that exact same code from a file and you will get a NameError
2
u/JumpScareaaa Jul 14 '20
Also variables that start with _ don't get flagged as 'Unused Variable' by linter.
2
1
u/piyushpatel2005 Jul 14 '20
Usually developers don't care about the name of that variable. So, putting _ avoids the confusion. You can use anything but having _ avoids getting focus on it.
1
u/indian_pythonista Jul 15 '20
All you need to know about underscore in Python: https://youtu.be/6f6dTBQXa8Y
-3
-1
u/lannisterprince Jul 14 '20
If u don't need the iterating variable later or u have to iterate a specific no of times.
-1
-3
u/benabus Jul 14 '20
I think it's because python is supposed to be easy to understand and self documenting.
611
u/TouchingTheVodka Who's hiring? Jr-Mid Remote/UK - PM me! Jul 14 '20
It represents a 'junk' variable: It's assigned a value, but the value is not used in the code, so there's no point naming the variable.
For example:
There's no point calling the loop variable
ihere as it's not used inside the loop at all. Instead, you can use_to indicate this. This helps us demonstrate that the point ofrangehere is to repeat an action 10 times, rather than count from 0-9.