ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] # 没有`__all__` study.py ~~~ class Test(object): def test(self): print("---Test类中的test函数---") def test1(): print("---test1函数---") def test2(): print("---test2函数---") ~~~ main.py ~~~ from study import * a = Test() a.test() test1() test2() ~~~ 输出 ~~~ ---Test类中的test函数--- ---test1函数--- ---test2函数--- ~~~ # 有`__all__` ~~~ __all__ = ["Test", "test1"] class Test(object): def test(self): print("---Test类中的test函数---") def test1(): print("---test1函数---") def test2(): print("---test2函数---") ~~~ ~~~ from study import * a = Test() a.test() test1() test2() ~~~ 输出 ~~~ Traceback (most recent call last): ---Test类中的test函数--- File "/Users/jdxia/Desktop/study/py/Car.py", line 8, in <module> ---test1函数--- test2() NameError: name 'test2' is not defined ~~~ 如果一个文件中有`__all__`变量,那么也就意味着这个变量中的元素,会被`from xxx import *`时导入 test2不在里面,所以不能使用