原文:http://igorsobreira.com/2010/09/16/difference-between-one-underline-and-two-underlines-in-python.html
"_"單下劃線
Python中不存在真正的私有方法。為了實(shí)現(xiàn)類似于c++中私有方法,可以在類的方法或?qū)傩郧凹右粋€“_”單下劃線,意味著該方法或?qū)傩圆粦?yīng)該去調(diào)用炮赦,它并不屬于API。
在使用property時样勃,經(jīng)常出現(xiàn)這個問題:
classBaseForm(StrAndUnicode):
? ? ?def_get_errors(self):
? ? ? "Returns an ErrorDict for the data provided for the form"
? ? ? ?if self._errors is None:
? ? ? ? ? ? ? self.full_clean()
? ? ? ?return self._errors
errors= property(_get_errors)
這里的errors是一個屬性眼五,屬于API的一部分,但是_get_errors是私有的彤灶,是不應(yīng)該訪問的,但可以通過errors來訪問該錯誤結(jié)果批旺。
"__"雙下劃線
用來避免子類覆蓋其內(nèi)容幌陕。
classA(object):
? ?def__method(self):
? ? ? print"I'm a method in A"
? ?def method(self):
? ? ? self.__method()?
a = A()?
a.method()
輸出是這樣的:
I'm a method in A
我們給A添加一個子類,并重新實(shí)現(xiàn)一個__method:
classB(A):
? ?def__method(self):
? ? ? print"I'm a method in B"
b=B()
b.method()
輸出:
I'm a method in A
就像我們看到的一樣汽煮,B.method()不能調(diào)用B.__method的方法搏熄。實(shí)際上,它是"__"兩個下劃線的功能的正常顯示暇赤。
因此心例,在我們創(chuàng)建一個以"__"兩個下劃線開始的方法時,這意味著這個方法不能被重寫鞋囊,它只允許在該類的內(nèi)部中使用止后。
在Python中如是做的?很簡單,它只是把方法重命名了译株,如下:
a =A()
a._A__method()#never use this!! please!
$ python example.py
I'm a method in A
如果你試圖調(diào)用a.__method瓜喇,它還是無法運(yùn)行的,就如上面所說歉糜,只可以在類的內(nèi)部調(diào)用__method乘寒。
"__xx__"前后各雙下劃線
當(dāng)你看到"__this__"的時,就知道不要調(diào)用它匪补。為什么伞辛?因?yàn)樗囊馑际撬怯糜赑ython調(diào)用的,如下:
>>> name ="igor"
>>> name.__len__() 4?
>>> len(name) 4?
>>> number = 10?
>>> number.__add__(20) 30?
>>> number + 20 30
“__xx__”經(jīng)常是操作符或本地函數(shù)調(diào)用的magic methods夯缺。在上面的例子中蚤氏,提供了一種重寫類的操作符的功能。
在特殊的情況下喳逛,它只是python調(diào)用的hook瞧捌。例如,__init__()函數(shù)是當(dāng)對象被創(chuàng)建初始化時調(diào)用的;__new__()是用來創(chuàng)建實(shí)例润文。
classCrazyNumber(object):
? ? ?def__init__(self, n):
? ? ? ? ?self.n=n
def__add__(self, other):returnself.n -otherdef__sub__(self, other):returnself.n +otherdef__str__(self):returnstr(self.n)
num= CrazyNumber(10)printnum#10printnum + 5#5printnum - 20#30