Looking through some of the Django code at authentication forms I noticed the following syntax
label=_("Username")
Normally I would have just used a pair of quotes around the string. Can someone exaplain to me what the underscore and parenthesis around "Username" do?
A single leading underscore in front of a variable, a function, or a method name means that these objects are used internally. This is more of a syntax hint to the programmer and is not enforced by the Python interpreter which means that these objects can still be accessed in one way on another from another script.
The use of double underscore ( __ ) in front of a name (specifically a method name) is not a convention; it has a specific meaning to the interpreter. Python mangles these names and it is used to avoid name clashes with names defined by subclasses.
The Python interpreter modifies the variable name with ___. So Multiple times It uses as a Private member because another class can not access that variable directly. The main purpose for __ is to use variable /method in class only If you want to use it outside of the class you can make it public.
Double underscores are used for fully private variables. If your class is intended to be subclassed, and you have attributes that you do not want subclasses to use, consider naming them with double leading underscores and no trailing underscores.
The _
is the name of a callable (function, callable object). It's usually used for the gettext
function, for example in Django:
from django.utils.translation import gettext as _ print _("Hello!") # Will print Hello! if the current language is English # "Bonjour !" in French # ¡Holà! in Spanish, etc.
As the doc says:
Python’s standard library gettext module installs
_()
into the global namespace, as an alias forgettext()
. In Django, we have chosen not to follow this practice, for a couple of reasons:[...]
The underscore character (
_
) is used to represent “the previous result” in Python’s interactive shell and doctest tests. Installing a global_()
function causes interference. Explicitly importinggettext()
as_()
avoids this problem.
Even if it's a convention, it may not be the case in your code. But be reassured, 99.9% of the time _
is an alias for gettext
:)
The underscore is just another Python object, but by convention the gettext
library scans for it to find translatable text.
Usually it is bound to the ugettext
callable:
from django.utils.translation import ugettext as _
See the translation chapter of the Django documentation:
Python’s standard library gettext module installs
_()
into the global namespace, as an alias forgettext()
. In Django, we have chosen not to follow this practice, for a couple of reasons:
- For international character set (Unicode) support,
ugettext()
is more useful thangettext()
. Sometimes, you should be usingugettext_lazy()
as the default translation method for a particular file. Without_()
in the global namespace, the developer has to think about which is the most appropriate translation function.- The underscore character (
_
) is used to represent “the previous result” in Python’s interactive shell and doctest tests. Installing a global_()
function causes interference. Explicitly importingugettext()
as_()
avoids this problem.
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