Beyonder
Beyonder

Reputation: 1475

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

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?

Upvotes: 22

Views: 10799

Answers (4)

Sandip
Sandip

Reputation: 79

_ indicates last valid output on the screen. System by default stores copy of the output to this _ variable. It does not applies to string that is printed using print function but I stores for the string stored into the variable.

enter image description here

Upvotes: 0

S.Lott
S.Lott

Reputation: 391952

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

Upvotes: 27

Nick T
Nick T

Reputation: 26727

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

Upvotes: 13

Mp0int
Mp0int

Reputation: 18737

this is used for gettext function, as is described here

Utf-8 support of django is good, so django handles it as unicodetext as described here

Upvotes: 6

Related Questions