當我導入main.py而不是運行它時,獲取此代碼塊的最佳方式是什麼?在python中,我如何獲得與__name__ =='__main__'相同的行爲:
#main.py
if __name__ == '__main__':
#Do something interesting.
#Do something to have the interesting code run after importing the file.
import main.py
main.__main__() # wrong of course.
爲什麼不直接在程序的主要部分定義'有趣的代碼'的函數呢? – si28719e 2012-01-06 08:44:10
[運行模塊時如何使__name__ =='__main__'可能重複](http://stackoverflow.com/questions/6114251/how-to-make-name-main-when-running-module) – 2012-01-06 08:49:13