Python零碎(一)


Python中的__name__和__main__含義詳解

在寫Python代碼和看Python代碼時,我們常常可以看到這樣的代碼:

1 def main():
2     ......
3  
4 if __name == "__main__":
5     main();

其中的函數名main,也可以是其他任意的,你所設置的名字。

這里,就出現了,我們此處所要解釋的

__name__和__main__

 

__name__:

如果是放在Modules模塊中,就表示是模塊的名字;

如果是放在Classs類中,就表示類的名字;

 

__main__:

模塊第一次被導出(import)后,系統會自動為其創建一個域名空間(namespace);

(模塊,都是有自己的名字的)此處的腳本的主模塊的名字,始終都叫做__main__。

 

代碼示例:

 1 #!/usr/bin/python
 2 # -*- coding: utf-8 -*-
 3 """
 4 【整理】Python中的__name__和__main__含義詳解
 5 http://www.crifan.com/python_detailed_explain_about___name___and___main__
 6  
 7 Version:    2012-11-17
 8 Author:     Crifan
 9 Contact:    http://www.crifan.com/contact_me/
10 """
11  
12 def square(x):
13     return x * x
14  
15 print "test: square(42) ==",square(42);

對應的,運行結果也是很正常的:

1 test: square(42) == 1764

但是呢,往往我們所見到的,和我們以后自己也會遇到的,自己寫的,有條理的,可復用的做法,那肯定是

那對應的square等函數,專門放到一個文件中,然后被別人調用,此時,就是這樣的:

對應的模塊文件是mymath.py,里面有我們實現的函數square:

 1 #!/usr/bin/python
 2 # -*- coding: utf-8 -*-
 3 """
 4 【整理】Python中的__name__和__main__含義詳解
 5 http://www.crifan.com/python_detailed_explain_about___name___and___main__
 6  
 7 Version:    2012-11-17
 8 Author:     Crifan
 9 Contact:    http://www.crifan.com/contact_me/
10 """
11  
12 def square(x):
13     return x * x
14  
15 print "test: square(42) ==",square(42);

然后別的python文件__name___and___main__.py中,導入此mymath模塊,然后使用其square函數:

 1 #!/usr/bin/python
 2 # -*- coding: utf-8 -*-
 3 """
 4 【整理】Python中的__name__和__main__含義詳解
 5 http://www.crifan.com/python_detailed_explain_about___name___and___main__
 6  
 7 Version:    2012-11-17
 8 Author:     Crifan
 9 Contact:    http://www.crifan.com/contact_me/
10 """
11  
12 import mymath;
13  
14 print "In caller, test for mymath: square(12)=",mymath.square(12);

然后運行結果是:

1 test: square(42) == 1764
2 In caller, test for mymath: square(12)= 144

此處,我們看到了,也同時出現了,原本用於mymath.py中去測試square函數的打印結果:

test: square(42) == 1764

而這樣的內容,很明顯,是作為我模塊的調用者,不希望看到的。也不關心的。

 

此時,我們所希望的是:

作為模塊mymath.py本身,希望有自己的相關的調試的代碼,用於調試自己模塊函數,演示如何使用等等代碼;

但是又不希望在被別的,本模塊的調用者,所執行到,所看到;

此時,就可以用上述的__main__來實現了:

把mymath.py改為:

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
【整理】Python中的__name__和__main__含義詳解
http://www.crifan.com/python_detailed_explain_about___name___and___main__
 
Version:    2012-11-17
Author:     Crifan
Contact:    http://www.crifan.com/contact_me/
"""
 
def square(x):
    return x * x
 
if __name__ == "__main__":
    print "test: square(42) ==",square(42);

此時:

1. 作為mymath.py本身,自己運行時,可以運行到此處的代碼,調試,驗證自己的函數square執行的是否正確:

1 test: square(42) == 1764

2.同時,作為mymath.py的調用者__name___and___main__.py,在import mymath的時候,也不會看到對應的代碼執行的結果了:

1 In caller, test for mymath: square(12)= 144

其中的__main__,就是:

作為模塊mymath.py本身:

作為腳本自己去運行的話,對應的模塊名,就是上面所解釋的,始終叫做__main__

關於這點,上述代碼已經驗證過了。因為mymath.py中的__name__,就是對應的,內置的變量,通過判斷,的確是__main__,然后才會去執行到對應的模塊的測試代碼的。

     如果被當做一個模塊被別人調用的時候,對應的模塊mymath.py的模塊名,就是mymath;

     關於這點,我們可以來驗證一下,把__name___and___main__.py改為:

 1 #!/usr/bin/python
 2 # -*- coding: utf-8 -*-
 3 """
 4 【整理】Python中的__name__和__main__含義詳解
 5 http://www.crifan.com/python_detailed_explain_about___name___and___main__
 6  
 7 Version:    2012-11-17
 8 Author:     Crifan
 9 Contact:    http://www.crifan.com/contact_me/
10 """
11  
12 import mymath;
13  
14 print "In caller, test for mymath: square(12)=",mymath.square(12);
15 print "As module be imported, mymath module name=",mymath.__name__;

再去運行,就可以看到輸出的結果是mymath了:

1 In caller, test for mymath: square(12)= 144
2 As module be imported, mymath module name= mymath

 

=========================================

python 調用Qt Creator創建的ui文件:

1 from PyQt4 import QtGui,QtCore,uic  # pyqt4版本
2 app=QtGui.QApplication(sys.argv)
3 qtCreatorFile='~~\demo\login.ui'  #***.ui文件所在具體路徑
4 loginui=uic.loadUi(qtCreatorFile)  #通過ui文件路徑調用ui文件
5 loginui.show()  #顯示控件
6 #該種引用方式屬於直接調用和顯示已經制作完畢的窗體控件,無法再對控件屬性做進一步操作和修改。若要更改控件屬性,需要將ui文件轉換成代碼文件,並將代碼中控件屬性變量修改為全局變量使得外部可以訪問

PYQT5 GUI開發窗口類型QApplication的調用流程:

1 app = QtWidgets.QApplication(sys.argv)  #  首先必須實例化QApplication類,作為GUI主程序入口
2 
3 aw = MainWindows()    # 實例化QtWidgets.QMainWindow類,創建自帶menu的窗體類型QMainWindow
4 aw.setWindowTitle("QMainWindow demo")   # 設置窗口標題
5 aw.show()   # 顯示窗體
6 sys.exit(app.exec_())   #當來自操作系統的分發事件指派調用窗口時,應用程序開啟主循環(mainloop)過程,當窗口創建完成,需要結束主循環過程,這時候呼叫sys.exit()方法來,結束主循環過程退出,並且釋放內存。為什么用app.exec_()而不是app.exec()?因為exec是python系統默認關鍵字,為了以示區別,所以寫成exec_   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM