我需要执行一个文件,所以我想知道它是否像这样工作?需要执行的文件将创建一个数据库。
答案 0 :(得分: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.')