Skip to main content
View All
Search
Shelves
Books
Sign up
Log in
Info
Content
Books
Python中if __name__ == ...
五、常见误区说明
五、常见误区说明
文件名影响
:假设
__name__
包含文件路径(实际只与导入名称相关)
错误比较
:写成
__name__ == "test.py"
(正确应为
__name__ == "__main__"
)
过度使用
:将全部代码放在main块中(应只包含执行入口)
Enter section select mode
Previous
四、最佳实践示例
Next
六、扩展应用:命令行参数处理
Back to top