【问题标题】:Is it generally a good idea to put a console client into module's __name__ == __main__ section? [closed]将控制台客户端放入模块的 __name__ == __main__ 部分通常是一个好主意吗? [关闭]
【发布时间】:2014-11-11 22:42:52
【问题描述】:

将控制台客户端放入模块的if __name__ == "__main__": 部分通常是个好主意吗?例如。设置 argparser 和预处理用户输入的代码。

【问题讨论】:

  • 基本上归结为程序需要做什么。出于测试目的,最好将内容移至 def main() 函数。

标签: python


【解决方案1】:

这样,程序将有效地兼作模块和脚本。这本身没有任何问题。 pdb 标准模块是一个示例,例如使用win32serviceutil.ServiceFramework 作为 Windows 服务运行的程序通常采用这种方式。

您唯一关心的是,如果您有多个脚本和/或模块,您仍然可以分辨出哪个是哪个。相关讨论请参见Is a Scripts directory an anti-pattern in Python? If so, what's the right way to import?。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 2011-09-25
    • 1970-01-01
    相关资源
    最近更新 更多