Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of '_' in python?

Tags:

When reading source code of Django, I find some statements:

class Field(object):       """Base class for all field types"""       __metaclass__ = LegacyConnection        # Generic field type description, usually overriden by subclasses     def _description(self):         return _(u'Field of type: %(field_type)s') % {             'field_type': self.__class__.__name__         }         description = property(_description)   class AutoField(Field):     description = _("Integer") 

I know it set description as 'Integer', but don't understand the syntax: description = _("Integer").
Can some one help on it?

like image 497
Beyonder Avatar asked Oct 19 '10 10:10

Beyonder


2 Answers

Please read up on Internationalization (i18n)

http://docs.djangoproject.com/en/dev/topics/i18n/

The _ is a commonly-used name for the function that translates strings to another language.

http://docs.djangoproject.com/en/dev/topics/i18n/translation/#standard-translation

Also, read all of these related questions on SO:

https://stackoverflow.com/search?q=%5Bdjango%5D+i18n

like image 182
S.Lott Avatar answered Sep 28 '22 17:09

S.Lott


Not an answer to your case but the more general "What's the meaning of '_' in python?":

In interactive mode, a _ will return the last result that wasn't assigned to a variable

>>> 1 # _ = 1 1 >>> _ # _ = _ 1 >>> a = 2 >>> _ 1 >>> a # _ = a 2 >>> _ # _ = _ 2 >>> list((3,)) # _ = list((3,)) [3] >>> _ # _ = _ [3] 

Not sure, but it seems like every expression that's not assigned to a variable is actually assigned to _.

like image 23
Nick T Avatar answered Sep 28 '22 17:09

Nick T