20 Python __all__
来源:互联网 发布:中国网络黑客事件 编辑:程序博客网 时间:2024/05/17 00:56
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 = 2b_no_all = 3_b_no_all = 4class Foo_no_all(object): a = 0def 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 = 4class Foo_have_all(object): a = 0def 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? Falseb_no_all import? True_b_no_all import? FalseFoo_no_all import? Truefun_no_all import? Truea_no_all = 1b_no_all = 3Foo_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? Trueb_have_all import? False_b_have_all import? FalseFoo_have_all import? Truefun_have_all import? False_a_have_all = 2Foo_have_all = <class 'A_have_all.Foo_have_all'>'''
在__init__.py
中定义__all__
文件位置
__init__.py
为空test.py代码如下
from packet import * #执行packet目录下的__init__.pyfor 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_allA_have_all,A_have_all中的成员,仅导入了A_have_all中__all__中的成员Foo_no_allfun_no_alla_no_allb_no_allA_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_allA_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_alla_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 #导入的是变量,不是文件名,变量,函数,类的优先级>模块
- 20 Python __all__
- 【python】__all__
- Python __all__
- python __all__
- Python – __all__ function
- python __all__的用法
- python中的__all__和__slots__
- python关于__all__的定义
- python中的__all__与__import__
- 【Python】用 __all__ 暴露接口
- python中的__all__和__slots__
- python里__all__ 属性用法
- python中的__all__和__slots__
- Python 中 __all__ 的作用
- __all__
- Python中的 __all__和__path__ 解析
- python的moudles文件中__all__作用
- python的moudles文件中__all__作用
- 什么是hibernate懒加载?什么时候用懒加载?为什么要用懒加载?
- LruMemChunk SharedMemCache::getLru().end()
- 解开 Windows 下的临界区中的代码死锁
- io流
- Mysql-索引覆盖
- 20 Python __all__
- Python深入02 上下文管理器
- 整理nodejs中的exports 和 module.exports的关系
- 微信小程序开发尝试
- Data Breach Report
- 【JS】alert()、confirm()、prompt()的区别
- typedef学习整理
- Error:java.lang.RuntimeException: Some file crunching failed, see logs for details
- 【52.49%】【codeforces 556A】Case of the Zeros and Ones