私信  •  关注

Zain

Zain 最近创建的主题
Zain 最近回复了

当您以交互方式运行Python时 __name__ 变量被赋值为 __main__ . 同样,当您从命令行执行一个Python模块,而不是将其导入另一个模块时,它的 __姓名__ 属性的值为 __主要__ ,而不是模块的实际名称。这样,模块可以查看自己的 __姓名__ 值来自行确定如何使用它们,是作为对另一个程序的支持,还是作为从命令行执行的主应用程序。因此,以下习惯用法在Python模块中非常常见:

if __name__ == '__main__':
    # Do something appropriate here, like calling a
    # main() function defined elsewhere in this module.
    main()
else:
    # Do nothing. This module has been imported by another
    # module that wants to make use of the functions,
    # classes and other useful bits it has defined.