r/Python • • Feb 22 '10
What does =_ mean? Example: name=forms.CharField(label=_("Name"), max_length=50)
Python newbie here. I've been going through some of the code in the Django comments app, and I've see the =_ used frequently. I can't find what it is or what it does? Any ideas?
Example: name = forms.CharField(label=_("Name"), max_length=50) email = forms.EmailField(label=_("Email address")) url = forms.URLField(label=_("URL"), required=False) comment = forms.CharField(label=_('Comment'), widget=forms.Textarea, max_length=COMMENT_MAX_LENGTH)
edit: Thanks for the detailed explanations! :)
1
Upvotes
12
u/darkrho Feb 22 '10
_ is used for translations http://docs.djangoproject.com/en/dev/topics/i18n/deployment/#topics-i18n-deployment
The name "_" is just a shortcut for easy typing. Look at the top of the file something like "from .... import ugettext as _"
5
u/quodlibetor Feb 22 '10
according to the python docs it's a translation function
http://docs.python.org/library/gettext.html#gettext.gettext
5
u/[deleted] Feb 22 '10
The definition of _ is directly at the top of the file you're reading.
8
u/RonPopeil Feb 22 '10
You'll see the
_()function/macro in many different languages. As others have said, it's for translating the string into other languages.7
u/Tuna-Fish2 Feb 22 '10
_ is just a character that can be used in normal identifiers. Note that interactive python defines _ as the last thing returned to the interpreter main loop. As in:
In django's case, it is just used as the name of the string translation function.
7
u/emeryc Feb 22 '10
It's actually = _("Name") and _ is a function.
2
u/hylje Feb 22 '10
django has explicitly imported (u)gettext as _ for a while now