for _ in list中的下划線_只是一個臨時的循環變量, 因為_是合法的變量名
for _ in [1, 2, 3]: print(_)
li = [[0, 1] for _ in range(3)] tu1 = ([0, 1] for _ in range(3)) # ()返回生成器, 而不是列表 tu2 = list([0, 1] for _ in range(3)) print(li) print(tu1) print(tu2) ''' 輸出結果: [[0, 1], [0, 1], [0, 1]] <generator object <genexpr> at 0x000000000214DF68> [[0, 1], [0, 1], [0, 1]] '''
下面將詳細的討論以下Python中下划線_的使用
一、 單個下划線直接做變量名(_)
主要有三種情況:
1. 解釋器中
_符號是指交互解釋器中最后一次執行語句的返回結果。這種用法最初出現在CPython解釋器中,其他解釋器后來也都跟進了。
2. 作為名稱使用
這個跟上面有點類似。_用作被丟棄的名稱。按照慣例,這樣做可以讓閱讀你代碼的人知道,這是個不會被使用的特定名稱。舉個例子,你可能無所謂一個循環計數的值:
n = 42 for _ in range(n): do_something()
3.國際化
_還可以被用作函數名。這種情況,單下划線經常被用作國際化和本地化字符串翻譯查詢的函數名。這種慣例好像起源於C語言。舉個例子,在 Django documentation for translation 中你可能會看到:
from django.utils.translation import ugettext as _ from django.http import HttpResponse def my_view(request): output = _("Welcome to my site.") return HttpResponse(output)
第二種和第三種用法會引起沖突,所以在任意代碼塊中,如果使用了_作國際化翻譯查詢函數,就應該避免再用作被丟棄的變量名。
二、 單下划線前綴的名稱(例如_shahriar)
以單下划線做前綴的名稱指定了這個名稱是“私有的”。在有些導入import * 的場景中,下一個使用你代碼的人(或者你本人)會明白這個名稱僅內部使用。Python documentation里面寫道:
a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.
之所以說在在 有些 import * 的場景,是因為導入時解釋器確實對單下划線開頭的名稱做了處理。如果你這么寫from import *,任何以單下划線開頭的名稱都不會被導入,除非模塊/包的__all__列表明確包含了這些名稱。更多相關信息見““Importing * in Python”。
三、雙下划線前綴的名稱(例如__shahriar)
以雙下划線做前綴的名稱(特別是方法名)並不是一種慣例;它對解釋器有特定含義。Python會改寫這些名稱,以免與子類中定義的名稱產生沖突。Python documentation中提到,任何__spam這種形式(至少以兩個下划線做開頭,絕大部分都還有一個下划線做結尾)的標識符,都會文本上被替換為_classname__spam,其中classname是當前類名,並帶上一個下划線做前綴。
class A(object): def _internal_use(self): # 單下划線 pass def __method_name(self): # 雙下划線 pass print(dir(A)) ''' 輸出結果: ['_A__method_name', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_internal_use'] '''
正如所料,_internal_use沒有變化,但__method_name被改寫成了_ClassName__method_name。現在創建一個A的子類B(這可不是個好名字),就不會輕易的覆蓋掉A中的__method_name了:
class A(object): def _internal_use(self): pass def __method_name(self): pass class B(A): def __method_name(self):pass print(dir(B)) ''' 輸出結果: ['_A__method_name', '_B__method_name', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_internal_use'] '''
四、名稱前后的雙下划線(如:__init__)
這種用法表示Python中特殊的方法名。其實,這只是一種慣例,對Python系統來說,這將確保不會與用戶自定義的名稱沖突。通常,你將會覆寫這些方法,並在里面實現你所需要的功能,以便Python調用它們。例如,當定義一個類時,你經常會覆寫“__init__”方法。雖然也可以編寫自己的特殊方法名,但不要這樣做。
class C: def __init__(self,name): self.name = name def __showname(self): pass print(dir(C)) ''' ['_C__showname', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] '''