簡體   English   中英

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

[英]What's the meaning of '_' in python?

在閱讀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")

我知道它將描述設置為'Integer',但不理解語法: description = _("Integer")
有人可以幫忙嗎?

請閱讀國際化(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

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

交互模式下_將返回未分配給變量的最后結果

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

不確定,但似乎每個未分配給變量的表達式實際上都分配給_

這是用於gettext的功能,如描述在這里

UTF-8支持的Django的好,所以Django的將其處理為unicodetext描述這里

_表示屏幕上的最后一個有效輸出。 系統默認情況下將輸出的副本存儲到此_變量。 它不適用於使用print函數打印的字符串,但我存儲了存儲在變量中的字符串。

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM