r/Python • u/[deleted] • 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! :)
5
u/quodlibetor Feb 22 '10
according to the python docs it's a translation function
2
u/themissinglint Feb 22 '10
That's only listed as common use, as in the example _ = gettext.gettext # ... print _('This is a translatable string.') where it's just been defined explicitly.
1
Feb 23 '10
Thanks guys, it all makes sense now! I wondered why all form templates were importing l18n custom tags.
5
Feb 22 '10
from django.utils.translation import ungettext, ugettext_lazy as _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:
>>> 1+2
3
>>> _
3In django's case, it is just used as the name of the string translation function.
4
u/self git push -f Feb 22 '10
Note that interactive python defines _ as the last thing returned to the interpreter main loop.
Wow, where is that documented?
I knew that Lisp environments use
*,**, and***to refer to the most recent three things retured to the repl, but not Python's_.2
u/Tuna-Fish2 Feb 22 '10
Wow, where is that documented?
No idea. I found that one day when I was bored and went trough builtins with dir() and help().
2
u/goodger Feb 22 '10
It's defined in http://docs.python.org/dev/tutorial/introduction.html, near the end of section 3.1.1:
"In interactive mode, the last printed expression is assigned to the variable _."
Also here: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#interactive
2
Feb 22 '10
Another curious fact about Python's REPL is:
>>> for i in range(3): str(i) '0' '1' '2'The
POPopcodes (used to discard the results of expressions-used-as-statements) are converted to a special opcode that printsrepr(notstr!) of the result. That's why, by the way,Noneis never printed implicitly in REPL: because every toplevel function call would then litter the output with Nones.This thing is kinda useful when writing doctests.
7
2
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 _"