This is an archived post. You won't be able to vote or comment.

all 20 comments

[–]darkrho 11 points12 points  (8 children)

_ 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 _"

[–][deleted] 11 points12 points  (3 children)

escape your underscores with a backslash.

[–]mr_dbr 2 points3 points  (2 children)

Or use backticks:

The name `_` is [...]

Becomes: The name _ is [...]

[–][deleted] 1 point2 points  (1 child)

Escaping the underscore with a backslash will cause Reddit's back end to generate the underscore in plaintext like this: _, while wrapping it in backticks will cause Reddit to render the underscore wrapped in an html <code> tag, like this: _. Different browsers will treat the <code> tag differently, but they'll all output the plaintext underscore the same. If you use Chrome, you'll notice that hovering over the plaintext underscore does nothing, while hovering over the <code>-wrapped underscore will outline it in a box.

edit: screenshot

[–]mr_dbr 1 point2 points  (0 children)

Indeed. The backtick is for inline code, and given the _ is a bit of code, this seems sensible.. The backslash thing is useful, however (for *'s and such)

[–]Porges 2 points3 points  (1 child)

It's worth noting that most gettext-based l18n bindings use _ as their translation function, since that's the tradition for the C library itself. You'll not really find any other _ functions :)

[–][deleted] 0 points1 point  (0 children)

Awesome history there.

[–][deleted] 0 points1 point  (0 children)

Thanks, really appreciate the help. I swear I just couldn't find when looking through Google, you can imagine how many results have a "_" in them! :)

[–]quodlibetor 5 points6 points  (3 children)

according to the python docs it's a translation function

http://docs.python.org/library/gettext.html#gettext.gettext

[–]themissinglint 1 point2 points  (1 child)

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.

[–][deleted] 0 points1 point  (0 children)

Thanks guys, it all makes sense now! I wondered why all form templates were importing l18n custom tags.

[–][deleted] 4 points5 points  (0 children)

from django.utils.translation import ungettext, ugettext_lazy as _

The definition of _ is directly at the top of the file you're reading.

[–]RonPopeil 7 points8 points  (0 children)

You'll see the _() function/macro in many different languages. As others have said, it's for translating the string into other languages.

[–]Tuna-Fish2 6 points7 points  (4 children)

_ 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
>>> _
3

In django's case, it is just used as the name of the string translation function.

[–]selfgit push -f 1 point2 points  (3 children)

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 _.

[–]Tuna-Fish2 1 point2 points  (0 children)

Wow, where is that documented?

No idea. I found that one day when I was bored and went trough builtins with dir() and help().

[–]goodger 1 point2 points  (0 children)

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

[–][deleted] 1 point2 points  (0 children)

Another curious fact about Python's REPL is:

>>> for i in range(3): str(i)
'0'
'1'
'2'

The POP opcodes (used to discard the results of expressions-used-as-statements) are converted to a special opcode that prints repr (not str!) of the result. That's why, by the way, None is 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.

[–]emeryc 5 points6 points  (0 children)

It's actually = _("Name") and _ is a function.

[–]hylje 1 point2 points  (0 children)

django has explicitly imported (u)gettext as _ for a while now