经常我们会编写自定义模块,用于被别的脚本调用;有时候为了方便,会使用from module_name import *的方式导入,这样会把模块中全部对象导入进来;
使用__all__结合列表,可以控制使用上述方式的时候,之后哪些对象可以被导入和引用;
use_all.py __all__ = ['name','gender','test'] name = 'charles' age = 23 gender = 'male' def test(): return 'This is a test'
限制只有name,gender和test可以被导入;
from use_all import * print name,gender,test()
注意:__all__只是针对 from module_name import *的方式有效;