• 技术文章 >Python技术 >Python基础教程

    Python不同模块下特殊变量的使用

    小妮浅浅小妮浅浅2021-09-06 10:01:21原创2032

    1、当模块(源文件)作为主程序运行,解释器将硬编码字符串赋值"__main__"给__name__变量。

    # It's as if the interpreter inserts this at the top
    # of your module when run as the main program.
    __name__ = "__main__"

    2、当模块被另一个导入时,导入语句中的名称分配给__name__变量。

    假设某个其他模块是主程序并且它导入您的模块。这意味着在主程序或主程序导入的其他模块中有这样的语句:

    # Suppose this is in some other main program.
    import foo

    解释器将搜索您的foo.py文件(以及搜索其他一些变体),并且在执行该模块之前。

    # It's as if the interpreter inserts this at the top
    # of your module when it's imported from another module.
    __name__ = "foo"

    以上就是Python不同模块下特殊变量的使用,希望对大家有所帮助。更多Python学习指路:python基础教程

    本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

    专题推荐:python模块
    品易云
    上一篇:Python解释器如何读取源文件 下一篇:Python DataFrame如何根据列值选择行

    相关文章推荐

    • python中的wxPython模块是什么?• python模块有哪些内置属性?• python模块引入的三种方式• python模块的搜索路径如何理解?• Python模块如何实现简单调用• python模块的搜索路径和顺序• dir()函数在python模块的使用• python模块中导入函数的方式• python模块中判断全局变量的赋值• python模块的介绍和导入• python模块如何传入参数• python模块如何安装• python模块的搜索顺序分析

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网