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 * 导入模块时,

  1. 没有定义__all__

    将导入模块中所有不以下划线(‘_’)开头的成员。

  2. 定义了__all__

    1) 除了__init__.py以外的文件中定义__all__

    ​ 将导入__all__列表里的类、函数、变量等成员;

    2) 在__init__.py中定义了__all__

    ​ 将导入__all__列表里的类、函数、变量、模块等成员;

    ​ 搜索顺序:首先搜索类、函数、变量,如果未找到,则在当前路径下搜索文件;

    导入模块时,如果未找到__all__中的成员,抛出AttributeError。

注意:

  1. __all__ 只影响from module import * 这种导入方式,对from module import something 没有影响;
  2. 一个文件中定义了多个__all__时,以 后编译的__all__为准;

测试code

两个python文件

  1. 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
  2. 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__

  1. 导入没有定义__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>'''
  2. 导入定义了__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__

文件位置

这里写图片描述

  1. __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导入。

  2. __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中的成员,导入了不以_开头的成员...'''
  3. __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__时才会搜索文件…

  4. __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__中的两个成员变量...'''
  5. 修改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        #导入的是变量,不是文件名,变量,函数,类的优先级>模块
0 0
原创粉丝点击