20 Python __all__
2016-12-11 19:40
274 查看
Python __all__
Python __all____all__的作用
测试code
除了__init__py以外的文件中定义__all__
在__init__py中定义__all__
转载请标明出处(http://blog.csdn.net/lis_12/article/details/53574885).
__all__的作用
使用from module import *导入模块时,
没有定义
__all__
将导入模块中所有不以下划线(‘_’)开头的成员。
定义了
__all__
1) 除了
__init__.py以外的文件中定义
__all__
将导入
__all__列表里的类、函数、变量等成员;
2) 在
__init__.py中定义了
__all__
将导入
__all__列表里的类、函数、变量、模块等成员;
搜索顺序:首先搜索类、函数、变量,如果未找到,则在当前路径下搜索文件;
在导入模块时,如果未找到
__all__中的成员,抛出AttributeError。
注意:
__all__只影响
from module import *这种导入方式,对
from module import something没有影响;
一个文件中定义了多个
__all__时,以 后编译的
__all__为准;
测试code
两个python文件A_no_all.py
#!/usr/bin/python # -*- coding: utf-8 -*- a_no_all = 1 _a_no_all = 2 b_no_all = 3 _b_no_all = 4 class Foo_no_all(object): a = 0 def fun_no_all(): pass
A_have_all.py
'''验证是__all__是否与变量位置有关,结果证明无关...''' a_have_all = 1 _a_have_all = 2 __all__ = ['Foo_have_all','_a_have_all'] #如果里面的变量名不存在,别的文件导入时才会报错 b_have_all = 3 _b_have_all = 4 class Foo_have_all(object): a = 0 def fun_have_all(): pass
除了__init__.py
以外的文件中定义__all__
导入没有定义__all__的模块
from A_no_all import * no_all = ['a_no_all','_a_no_all','b_no_all','_b_no_all','Foo_no_all','fun_no_all'] for i in no_all: print '%-12s import?'%i,i in vars() d = globals() for i in no_all: if i in d: print '%s = %s'%(i,d[i]) '''以下划线开头的变量没有导入... a_no_all import? True _a_no_all import? False b_no_all import? True _b_no_all import? False Foo_no_all import? True fun_no_all import? True a_no_all = 1 b_no_all = 3 Foo_no_all = <class 'A_no_all.Foo_no_all'> fun_no_all = <function fun_no_all at 0x000000000332C198> '''
导入定义了
__all__的模块
from A_have_all import * have_all = ['a_have_all','_a_have_all','b_have_all','_b_have_all','Foo_have_all','fun_have_all'] for i in have_all: print '%-12s import?'%i,i in vars() d = globals() for i in have_all: if i in d: print '%s = %s'%(i,d[i]) '''仅导入了__all__中的成员,不管变量名是否以“_”开头 a_have_all import? False _a_have_all import? True b_have_all import? False _b_have_all import? False Foo_have_all import? True fun_have_all import? False _a_have_all = 2 Foo_have_all = <class 'A_have_all.Foo_have_all'> '''
在__init__.py
中定义__all__
文件位置__init__.py为空
test.py代码如下
from packet import * #执行packet目录下的__init__.py for i in globals().keys(): print i ''' __builtins__ __file__ __package__ __name__ __doc__ '''
从代码可知,未将A_no_all.py和A_have_all.py导入。
__init__.py内容如下
from A_have_all import * from A_no_all import *
test.py内容如下
from packet import * for i in globals.keys(): print i ''' Foo_have_all A_have_all,A_have_all中的成员,仅导入了A_have_all中__all__中的成员 Foo_no_all fun_no_all a_no_all b_no_all A_no_all,A_no_all中的成员,导入了不以_开头的成员 ... '''
__init__.py内容如下
__all__ = ['A_have_all','A_no_all']
test.py内容如下
from packet import * #此时与from packet import A_no_all,A_have_all等价 for i in globals().keys(): print i ''' A_no_all A_have_all ... ''' print A_no_all.__dict__ print A_have_all.__dict__
如果在
__init__.py中定义了
__all__,首先搜索变量、函数、类,如果未找到,则在当前路径下搜索文件。
foo.py内容如下,foo.py与A_have_all.py在同一路径下
__all__ = ['A_have_all']
在别的文件导入foo
from foo import * #AttributeError: 'module' object has no attribute 'A_have_all'
以上代码可以证明,只有在
__init__.py定义了
__all__时才会搜索文件…
__init__.py内容如下
from A_have_all import * from A_no_all import *
__all__ = ['a_no_all','_a_have_all']#包含的是变量名
test.py内容如下
from packet import * for i in globals().keys(): print i ''' _a_have_all a_no_all,仅导入了__init__.py中__all__中的两个成员变量 ... '''
修改A_have_all,让变量名与文件名一致,内容如下
A_have_all = 1
__init__.py内容如下
from A_have_all import * __all__ = ['A_have_all']#与文件名字相同的变量名
test.py代码如下
from packet import * print A_have_all #导入的是变量,不是文件名,变量,函数,类的优先级>模块
相关文章推荐
- 1044. 火星数字(20)--Python
- “_imaging.c:75:20: 致命错误: Python.h:没有那个文件或目录 编译中断”解决方案
- _mysql.c:29:20: error: Python.h: No such file or directory
- Python实现基于朴素贝叶斯的垃圾邮件分类 标签: python朴素贝叶斯垃圾邮件分类 2016-04-20 15:09 2750人阅读 评论(1) 收藏 举报 分类: 机器学习(19) 听说
- 1047. 编程团体赛(20)--Python
- [leetcode]Substring with Concatenation of All Words @ Python
- pythontip 挑战python (11-20)
- maya-python-pyqt-delete-all-widgets-in-a-layout/
- 机器学习 Python 库 Top 20
- python re.findall 遇到的小问题
- Python Exercise #20
- python安装扩展”unable to find vcvarsall.bat“的解决办法
- LeetCode 30. Substring with Concatenation of All Words----Python实现
- python安装easy_insall工具
- 1009. 说反话 (20) Python 2 编译
- python---解决“Unable to find vcvarsall.bat”错误
- 笨方法学Python 习题 20: 函数和文件
- python---解决“Unable to find vcvarsall.bat”错误,在windows 64位上安装
- Python ElementTree的find,findall函数参数
- python基础课程_学习笔记20:标准库:有些收藏夹——os