社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

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.