Python Python中單下劃線和雙下劃線

2021-09-24 10:08:43 字數 777 閱讀 8986

class myclass():

… definit(self):

… self.__superprivate = 「hello」

… self._semiprivate = 「, world!」

…mc = myclass()

print mc.__superprivate

traceback (most recent call last):

file 「」, line 1, in

attributeerror: myclass instance has no attribute 『__superprivate』

print mc._semiprivate

, world!

print mc.dict

foo:一種約定,python內部的名字,用來區別其他使用者自定義的命名,以防衝突,就是例如__init__(),del(),call()這些特殊方法

_foo:一種約定,用來指定變數私有.程式設計師用來指定私有變數的一種方式.不能用from module import * 匯入,其他方面和公有一樣訪問;

__foo:這個有真正的意義:解析器用_classname__foo來代替這個名字,以區別和其他類相同的命名,它無法直接像公有成員一樣隨便訪問,通過物件名._類名__***這樣的方式可以訪問.

Python Python的單雙引號

python真的爽,單雙引號的運用太舒服了 在python中,使用單引號或雙引號是沒有區別的,都可以用來表示乙個字串 1.單雙引號都可以用來表達 輸入 print hello1 print hello2 輸出 hello1 hello22.還可以一起用,來避免混淆 輸入 print test the...

Python Python如何實現單例模式?

對於系統中的某些類來說,只有乙個例項很重要,例如,乙個系統中可以存在多個列印任務,但是只能有乙個正在列印的任務 乙個系統中只能有乙個串列埠管理器或檔案系統 乙個系統只能有乙個計時工具或id生成器。如在window是中就只能開啟乙個任務管理器。如果不使用機制對視窗物件進行唯一化,將彈出多個視窗,如果這...

python python中的遍歷

遍歷list集合 infp 1,2,3,3,3 for m in infp print m 值得注意的是,m在此處的值是infp中的想對應的值,當我們通過del infp m 或者 infp.remove m 如下 infp 1,2,3,3,3 for m in infp if m is 3 pri...