if __name__ ==“__main__”
时间: 2023-11-23 17:05:59 浏览: 92
if __name__ == "__main__"是Python中常见的一句代码,它在脚本文件中起到一个特殊的作用。当脚本文件直接被执行时,也就是作为主程序运行时,if __name__ == "__main__"后的代码块会被执行。而当脚本文件作为一个模块被导入时,if __name__ == "__main__"后的代码块则不会被执行。通过这种方式,可以使某些代码只在脚本文件被直接执行时运行,而在作为模块被导入时不运行。这样可以方便进行测试和模块的复用。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [if __name__ == ‘__main__’的用法](https://blog.csdn.net/CSDN_lzw99/article/details/124249393)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [Python中if __name__ == '__main__'作用解析](https://download.csdn.net/download/weixin_38503496/12876664)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文