首页 > 编程语言 > 详细

Python中__all__的作用

时间:2021-06-03 23:38:45      阅读:16      评论: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

  

Python中__all__的作用

原文:https://www.cnblogs.com/zukang/p/14846566.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!