python中'_'的含义是什么?

Bey*_*der 22 python django syntax

在阅读Django的源代码时,我发现了一些声明:

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")
Run Code Online (Sandbox Code Playgroud)

我知道它将描述设置为'整数',但不理解语法:description = _("Integer").
有人可以帮忙吗?

S.L*_*ott 27

请阅读国际化(i18n)

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

_是将字符串转换为另一种语言的函数的常用名称.

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

另外,在SO上阅读所有这些相关问题:

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


Nic*_*k T 13

不是你的案例的答案,而是更一般的"python中'_'的含义是什么?":

交互模式下,a _将返回未分配给变量的最后结果

>>> 1 # _ = 1
1
>>> _ # _ = _
1
>>> a = 2
>>> _
1
>>> a # _ = a
2
>>> _ # _ = _
2
>>> list((3,)) # _ = list((3,))
[3]
>>> _ # _ = _
[3]
Run Code Online (Sandbox Code Playgroud)

不确定,但似乎每个未分配给变量的表达式实际上都已分配给_.

  • 似乎在python程序中还有另一种'_'用法.如:I_need,_,I_need_2 =('a','b','c'); 在这种情况下,您不关心元组中的第二个值,因此它可以节省您为这些无用值设计一些变量名称的时间,从而使代码更易于阅读. (4认同)

Fal*_*gel 6

这是用于gettext的功能,如描述在这里

UTF-8支持的Django的好,所以Django的将其处理为unicodetext描述这里