2014-09-21 28 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.')