知行编程网知行编程网  2022-12-18 10:00 知行编程网 隐藏边栏  0 
文章评分 0 次,平均分 0.0
导语: 本文主要介绍了关于Python不同模块下特殊变量的使用的相关知识,希望可以帮到处于编程学习途中的小伙伴

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

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"


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

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享