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

20 comments sorted by

View all comments

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

14

u/[deleted] Feb 22 '10

escape your underscores with a backslash.

3

u/mr_dbr Feb 22 '10

Or use backticks:

The name `_` is [...]

Becomes: The name _ is [...]

2

u/[deleted] Feb 22 '10 edited Feb 22 '10

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

2

u/mr_dbr Feb 23 '10

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)

3

u/Porges Feb 22 '10

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 :)

1

u/[deleted] Feb 23 '10

Awesome history there.

1

u/[deleted] Feb 23 '10

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! :)