r/pythontips • u/CrayonConstantinople Mod • Jun 04 '16
Syntax Demystifying underscores in Python
One Underscore
A single underscore by itself is usually to convey a dummy variable that need not be used but was still encountered. This is merely a convention. An example of this is when iterating over an enumerated list and only the index is required, not the item. You may wish to convey in the code as follows:
for i, _ in enumerate(my_list):
print i
One Underscore Before Name
_variable This is semiprivate and indicates to others that this is a hidden variable. It appears hidden at first glance but can be both found and accessed easily by anyone using your code. Note that names starting with _ will not be imported via "from module import *".
Two Underscores Before Name
__variable This is often incorrectly considered superprivate, while it's actual meaning is just to namemangle to prevent accidental access. This can happen if a child class inherits from your class but __variable needs to be unique to both. Under the hood it is now:
_parentclass__variable and _childclass__variable.
Double Underscores (also known as dunder)
__variable__ This is typically reserved for builtin methods like __init__, __str__, etc.
Underscores used in numbers (as of Python 3.6)
As of Python 3.6, the underscore can be used as a separator in numeric values, to make them more readable:
>>>100_000_000 == 100000000
TrueEdit: Added more as they were pointed out in the comments. Thanks to u/SLGC, u/pvkooten, u/Underyx for the updates!
5
u/pvkooten Jun 04 '16
You forgot the meaning of a single _ :-)
1
Jun 04 '16
Tell us! Never seen it before
6
u/Zoccihedron Jun 04 '16
I don't believe it has a special meaning to the interpreter*, but by convention it means that the output being stored in that variable isn't going to be used.
# I'll admit this example is silly # but I can't think of a good one now def silly_example(): return 1, 2 # We only need the number 2 # and we don't want it to be in a tuple # so let's tell future developers looking # at this that we don't use the 1 _, a = silly_example()One consequence of this is that you should only see a single underscore on the left side of assignment. If you ever see it anywhere else, there's a chance you're doing something wrong. I believe the reasoning for this convention is that no one in their right mind would use a single underscore for a variable name for anything important.
* If it does have a special meaning, please correct me
5
u/schoolmonkey Jun 04 '16
In the interactive interpreter, _ is the value of the last line. It's kind of like every line has an imaginary _= at the beginning, if it wouldn't cause a syntax error.
4
u/Citrauq Jun 04 '16
_is also used by convention as a shorthand forgettexttranslation functions. For example, in django you will often see:from django.utils.translation import ugettext as _or
from django.utils.translation import ugettext_lazy as _2
u/mirthcontrol Jun 05 '16 edited Jul 16 '16
For a more practical example, how about polling with a range:
import time import requests for _ in range(60): r = requests.get(url) if r.status_code != 200 time.sleep(1) else: breakWe never use the variable extracted in the for loop, but something extremely functional happens around that variable.
2
u/jaked122 Jun 05 '16
I believe that this existed before as a placeholder in prolog, haskell, and ocaml. Of those three, prolog and haskell existed before python.
In those languages, it is used as a wildcard for pattern matching, which, I think, doesn't exist in python.
In any case, an underscored variable is exactly the same minus the import mechanism, when you
import * from <module>and therefore may be accessed explicitly.1
Jun 04 '16
That sounds kind of bad. In that case, can't you just use
[1]?5
u/ohaz Jun 04 '16
well, imagine a function returning 4 variables (e.g. points of a bounding box). If you only need the second and the fourth, writing _, a, _, b = bounding_box() is both shorter and faster than writing a, b = bounding_box()[1], bounding_box()[3]
4
u/Femaref Jun 04 '16
also it doesn't call the method twice. An example while keeping the semantics would be
bbox = bounding_box() a, b = bbox[1], bbox[3]1
3
u/TeamSpen210 Jun 05 '16
Another use of underscore: if a variable or parameter name is the same as a keyword, a trailing underscore is often used - func(class_=int). It's also used to avoid shadowing builtin names.
1
u/Underyx Jun 04 '16 edited Jun 07 '16
Also, as of Python 3.6, the underscore can be used as a separator in numeric values, to make them more readable:
>>> 100_000_000 == 100000000
True1
u/Airith Jun 04 '16
This is for Django:
In the Django source usually the ugettextlazy (for i18n translations) is imported as _ and then strings are created like so: _('john madden')
I'm not a big fan of this, I'd rather import it as trans (trans('john madden')) or something as I like using _ as a throwaway like in OP's example.
1
1
Jun 12 '16
It would help be helpful if you expand on the different common double underscore built in methods.
18
u/[deleted] Jun 04 '16
Also, names starting with _ will not be imported via "from module import *".