Python中__all__的作用

时间:2021-06-04 18:49:33   收藏:0   阅读:0

__all__ = [<string>]

它是一个string元素组成的list变量,定义了当你使用 from <module> import * 导入某个模块的时候能导出的符号(这里代表变量,函数,类等)。

其实就是代码保护,限定本模块中只有哪些能被import。

举例:foo.py

__all__ = [‘a‘, ‘b‘]

a = "a"
def b(): return ‘b‘
c = "c"

  现导入如下:

from foo import *

print a
print b

#The following will trigger an exception, as "c" is not exported by the module
print c

  

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!