Python __all__变量用法

1年前 (2024-04-27)
事实上,当我们向文件导入某个模块时,导入的是该模块中那些名称不以下划线(单下划线“_”或者双下划线“__”)开头的变量、函数和类。因此,如果我们不想模块文件中的某个成员被引入到其它文件中使用,可以在其名称前添加下划线。

以前面章节中创建的 demo.py 模块文件和 test.py 文件为例(它们位于同一目录),各自包含的内容如下所示:

#demo.py

def say():

print("人生苦短,我学Python!")

def CLanguage():

print("C语言中文网:http://c.biancheng网站站点" rel="nofollow" /> 人生苦短,我学Python!
C语言中文网:http://c.biancheng网站站点" rel="nofollow" /> 人生苦短,我学Python!
C语言中文网:http://c.biancheng网站站点" rel="nofollow" />

def say():

print("人生苦短,我学Python!")

def CLanguage():

print("C语言中文网:http://c.biancheng网站站点" rel="nofollow" /> 人生苦短,我学Python!
C语言中文网:http://c.biancheng网站站点" rel="nofollow" />

#demo.py

def say():

print("人生苦短,我学Python!")

def CLanguage():

print("C语言中文网:http://c.biancheng网站站点" rel="nofollow" /> 人生苦短,我学Python!
C语言中文网:http://c.biancheng网站站点" rel="nofollow" />

from demo import say

from demo import CLanguage

from demo import disPython

say()

CLanguage()

disPython()

运行 test.py,输出结果为:

人生苦短,我学Python!
C语言中文网:http://c.biancheng网站站点" rel="nofollow" />