转自:http://blog.csdn.net/sxingming/article/details/52903377

python模块中的__all__属性,可用于模块导入时限制,如:
from module import *
此时被导入模块若定义了__all__属性,则只有__all__内指定的属性、方法、类可被导入。

若没定义,则导入模块内的所有公有属性,方法和类 。

  1. # kk.py
  2. class A():
  3. def __init__(self,name,age):
  4. self.name=name
  5. self.age=age
  6. class B():
  7. def __init__(self,name,id):
  8. self.name=name
  9. self.id=id
  10. def func():
  11. print 'func() is called!'
  12. def func1():
  13. print 'func1() is called!'
  1. #test_kk.py
  2. from kk import *  #由于kk.py中没有定义__all__属性,所以导入了kk.py中所有的公有属性、方法、类
  3. a=A('python','24')
  4. print a.name,a.age
  5. b=B('python',123456)
  6. print b.name,b.id
  7. func()
  8. func1()

运行结果:
python 24
python 123456
func() is called!

func1() is called!

  1. #kk.py
  2. __all__=('A','func') #在别的模块中,导入该模块时,只能导入__all__中的变量,方法和类
  3. class A():
  4. def __init__(self,name,age):
  5. self.name=name
  6. self.age=age
  7. class B():
  8. def __init__(self,name,id):
  9. self.name=name
  10. self.id=id
  11. def func():
  12. print 'func() is called!'
  13. def func1():
  14. print 'func1() is called!'
  1. #test_kk.py
  2. from kk import *  #kk.py中定义了__all__属性,只能导入__all__中定义的属性,方法和类
  3. a=A('python','24')
  4. print a.name,a.age
  5. func()
  6. #func1() #NameError: name 'func1' is not defined
  7. #b=B('python',123456) #NameError: name 'B' is not defined

运行结果:

python 24
func() is called!

  1. #kk.py
  2. def func(): #模块中的public方法
  3. print 'func() is called!'
  4. def _func(): #模块中的protected方法
  5. print '_func() is called!'
  6. def __func():#模块中的private方法
  7. print '__func() is called!'
  1. #test_kk.py
  2. from kk import *  #这种方式只能导入公有的属性,方法或类【无法导入以单下划线开头(protected)或以双下划线开头(private)的属性,方法或类】
  3. func()
  4. #_func() #NameError: name '_func' is not defined
  5. #__func() #NameError: name '__func' is not defined

运行结果:
func() is called!

  1. __all__=('func','__func','_A') #放入__all__中所有属性均可导入,即使是以下划线开头
  2. class _A():
  3. def __init__(self,name):
  4. self.name=name
  5. def func():
  6. print 'func() is called!'
  7. def func1():
  8. print 'func1() is called!'
  9. def _func():
  10. print '_func() is called!'
  11. def __func():
  12. print '__func() is called!'
  1. from kk import *
  2. func()
  3. #func1() #func1不在__all__中,无法导入 NameError: name 'func1' is not defined
  4. #_func() #_func不在__all__中,无法导入  NameError: name '_func' is not defined
  5. __func() #__func在__all__中,可以导入
  6. a=_A('python') #_A在__all__中,可以导入
  7. print a.name

运行结果:

func() is called!
__func() is called!
python

  1. #kk.py
  2. def func():
  3. print 'func() is called!'
  4. def _func():
  5. print '_func() is called!'
  6. def __func():
  7. print '__func() is called!'
  1. #test_kk.py
  2. from kk import func,_func,__func  #可以通过这种方式导入public,protected,private
  3. func()
  4. _func() #NameError: name '_func' is not defined
  5. __func() #NameError: name '__func' is not defined

运行结果:
func() is called!
_func() is called!
__func() is called!

  1. #kk.py
  2. def func():
  3. print 'func() is called!'
  4. def _func():
  5. print '_func() is called!'
  6. def __func():
  7. print '__func() is called!'
  1. #test_kk.py
  2. import kk  #也可以通过这种方式导入public,protected,private
  3. kk.func()
  4. kk._func() #NameError: name '_func' is not defined
  5. kk.__func() #NameError: name '__func' is not defined

运行结果:
func() is called!
_func() is called!

__func() is called!

  1. #kk.py
  2. import sys
  3. __all__ = ["func"]  # 排除了 'sys'
  4. def func():
  5. print 'func() is called!'
  1. #test_kk.py
  2. from kk import *
  3. #print sys.path #NameError: name 'sys' is not defined
  4. func()

运行结果:
func() is called!
如果一个模块需要暴露的接口改动频繁,__all__ 可以这样定义:
__all__ = [
    "foo",
    "bar",
    "egg",
]
最后多出来的逗号在 Python 中是允许的,也是符合 PEP8 风格的。

模块中不使用__all__属性,则导入模块内的所有公有属性,方法和类 。

模块中使用__all__属性,则表示只导入__all__中指定的属性,因此,使用__all__可以隐藏不想被import的默认值。

__all__变量是一个由string元素组成的list变量。
它定义了当我们使用 from <module> import * 导入某个模块的时候能导出的符号(这里代表变量,函数,类等)。
from <module> import * 默认的行为是从给定的命名空间导出所有的符号(当然下划线开头的变量,方法和类除外)。
需要注意的是 __all__ 只影响到了 from <module> import * 这种导入方式,
对于 from <module> import <member> 导入方式并没有影响,仍然可以从外部导入。

(完)

python模块中的__all__属性的更多相关文章

  1. python学习笔记013——模块中的私有属性

    1 私有属性的使用方式 在python中,没有类似private之类的关键字来声明私有方法或属性.若要声明其私有属性,语法规则为: 属性前加双下划线,属性后不加(双)下划线,如将属性name私有化,则 ...

  2. python模块中的特殊变量

    37.模块的特殊变量:      显示模块中的变量 import s1 print(vars(s1))      1.__doc__:打印注释信息. #!/usr/bin/env python # _ ...

  3. 嵌入Python系列 | 调用Python模块中无参数函数

    开发环境 Python版本:3.6.4 (32-bit) 编辑器:Visual Studio Code C++环境:Visual Studio 2013 需求说明 在用VS2013编写的Win32程序 ...

  4. 嵌入Python | 调用Python模块中无参数的函数

    开发环境 Python版本:3.6.4 (32-bit) 编辑器:Visual Studio Code C++环境:Visual Studio 2013 需求说明 在用VS2013编写的Win32程序 ...

  5. Python - 模块中的"if __name__ == '__main__':"

    1.1 如果导入的模块除了定义函数之外还中有可以执行代码,那么Python解释器在导入这个模块时就会执行这些代码. module1.py: def foo(): print('module 1') f ...

  6. Python开发【第一篇】Python模块中特殊变量

    模块中特殊变量 生产环境中,常用的就是__name__和__file__ __doc__ __package__ __cached__ __name__ __file__ 一. __doc__  #获 ...

  7. python 模块中__all__作用

    test.py文件开头写上__all__=[func1,func2] 当其他文件导入  from test import * 只会导出"[func1,func2]"里面的,其他调用 ...

  8. 说说Python多线程中的daemon属性方法

    大家看多线程部分的时候肯定看到过daemon这个属性,当我在百度了一圈后也没发现有比较好的解释(或者大家对这个解释都非常清楚),于是自己通过代码和官方介绍了解它,进行了一些总结 给大家一些参考. 首先 ...

  9. python 模块中的 __init__.py __main__.py

    python中文件夹想作为一个模块被引用,则在文件夹内必须要包含 __init__.py 文件,即使此文件为空. 如果此模块想要运行则必须要包含 __main__.py 文件.接下来说下两个文件起到的 ...

随机推荐

  1. Hibernate自定义简单主键生成

    Hibernate自定义主键生成 当使用Hibernate定义pojo的时候,有时候需要生成一定规则的数据表主键,这时候我们可以采用自定义主键生成方式去生成主键. 例如: 1.在pojo属性中定义数据 ...

  2. mysql 时间戳格式化函数from_unixtime使用说明

    我们一般使用字段类型int(11)时间戳来保存时间,这样方便查询时提高效率.但这样有个缺点,显示的时间戳,非常难知道真实日期时间. mysql提供了一个时间戳格式化函数from_unixtime来转换 ...

  3. Android内存泄露之开篇

    先来想这三个问题 内存泄露是怎么回事 内存会泄露的原因 避免内存泄露 1.内存泄露怎么回事 一个程序中,已经不须要使用某个对象,可是由于仍然有引用指向它垃圾回收器就无法回收它,当然该对象占用的内存就无 ...

  4. HDU 5407 CRB and Candies(LCM +最大素因子求逆元)

    [题目链接]pid=5407">click here~~ [题目大意]求LCM(Cn0,Cn1,Cn2....Cnn)%MOD 的值 [思路]来图更直观: 这个究竟是怎样推出的.说实话 ...

  5. 兔子--CheckBox与Radiobutton的差别

    RadioButton和CheckBox的差别: 1.单个RadioButton在选中后.通过点击无法变为未选中状态,单个CheckBox在选中后.通过点击能够变为未选中. 2.一组RadioButt ...

  6. 1.1 Spring概述

        Spring是分层的Java SE/EE应用一站式的轻量开源框架,以 反转控制(Inverse of Control,IoC).面向切面编程(Aspect Oriented Programmi ...

  7. B. Error Correct System (CF Round #296 (Div. 2))

    B. Error Correct System time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  8. cocos2dx 在android平台打开文件问题

        我们有一个项目是基于cocos2dx + lua,在网络部分用到了protobuf, 在初始化protobuf的时候须要读取本地文件,用lua的io.open读取文件在windows,ios上 ...

  9. ORA-00907: 缺失右括号(通用解决办法)

    PL/SQL 的SQL语句可以执行,但是放在hibernate中,后台打印,出现了错误. 错误的SQL解析:黄色为错误部分 Hibernate:      select         examine ...

  10. HDU 3308 线段树单点更新+区间查找最长连续子序列

    LCIS                                                              Time Limit: 6000/2000 MS (Java/Oth ...