[英]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上閱讀所有這些相關問題:
不是你的案例的答案,而是更一般的“python中'_'的含義是什么?”:
在交互模式下 , _
將返回未分配給變量的最后結果
>>> 1 # _ = 1
1
>>> _ # _ = _
1
>>> a = 2
>>> _
1
>>> a # _ = a
2
>>> _ # _ = _
2
>>> list((3,)) # _ = list((3,))
[3]
>>> _ # _ = _
[3]
不確定,但似乎每個未分配給變量的表達式實際上都分配給_
。
_表示屏幕上的最后一個有效輸出。 系統默認情況下將輸出的副本存儲到此_變量。 它不適用於使用print函數打印的字符串,但我存儲了存儲在變量中的字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.