2014-09-21 50 views
0

我需要執行一個文件,所以我想知道它是否可以像這樣工作?需要執行的文件將創建一個數據庫。導入時是否會執行.py [python3]

+1

是嗎?如果代碼是正確的? 你可以更詳細地描述你的問題嗎? – Timo 2014-09-21 13:37:30

+0

@Timo好吧,沒有真正的問題,我想知道的是,如果我導入或不導入,它會被執行,我想你給了我一個答案,謝謝! – 2014-09-21 13:40:08

回答

1

好吧,我在這裏勾畫了答案。如果代碼沒有封裝在類或函數中,代碼將會運行。例如:

def f(): 
    print ('This line will not be executed during import from another file') 

print ('This line is executed!') 

if __name__ == '__main__': 
    print ('This line is executed only, if the program is main program, but not during import from another program.')