尧图精选

if __name__ == “__main__“ 讲解

🕒 发布时间:2026/9/5 5:46:35 📁 来源:尧图网络
Python 脚本有两种运行方式直接运行本文件python test.py内置变量__name__会被自动赋值为字符串__main__2 被其他文件导入import test__name__等于当前文件名模块名不会等于__main__。这样程序就不会一倒入就直接运行可以用来做测试代码。如下例子print(f当前 __name__ {__name__}) def hello(): print(hello) if __name__ __main__: hello()
上一篇/下一篇内容由系统自动关联 返回资讯列表 →