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

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

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

[python] view plaincopy
  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!'
[python] view plaincopy
  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!

[python] view plaincopy
  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!'
[python] view plaincopy
  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!

[python] view plaincopy
  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!'
[python] view plaincopy
  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!

[python] view plaincopy
  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!'
[python] view plaincopy
  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

[python] view plaincopy
  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!'
[python] view plaincopy
  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!

[python] view plaincopy
  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!'
[python] view plaincopy
  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!

[python] view plaincopy
  1. #kk.py
  2. import sys
  3. __all__ = ["func"]  # 排除了 'sys'
  4. def func():
  5. print 'func() is called!'
[python] view plaincopy
  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> 导入方式并没有影响,仍然可以从外部导入。

(完)

转载于:https://www.cnblogs.com/eternal1025/p/8531975.html

python模块中的__all__属性相关推荐

  1. Python培训教程分享:Python模块如何导入__all__属性?

    本期小编为大家带来的Python培训教程是关于"Python模块如何导入__all__属性?"的内容,后面在工作中是会遇到Python模块这个工作内容的,Python模块的开头通常 ...

  2. Matplotlib模块中没有verbose属性

    Matplotlib模块中没有verbose属性 Matplotlib是Python中广泛使用的数据可视化库,支持绘制各种图表.在使用Matplotlib时,有些用户可能会遇到"module ...

  3. Python类中的私有属性和私有方法

    Python类中的私有属性和私有方法 Python是面向对象的语言,在Python里一切皆对象,所以大部分情况下,我们都是通过类的方式来编程. 但是Python中的属性和方法在类的外部默认是可以访问的 ...

  4. python 模块命名空间_如何在python模块中执行导入而不污染其命名空间?

    我正在开发一个用于处理一些科学数据的Python包.在其他模块和包中有多个经常使用的类和函数,包括numpy,我几乎需要在包的任何模块中定义的每个函数. Pythonic的处理方式是什么?我已经考虑过 ...

  5. python面向对象中的私有属性和私有化方法

    xx: 公有变量 (公有) _x: 单前置下划线,私有化属性或方法,from somemodule import *禁止导入,类对象和子类可以访问 __xx:双前置下划线,避免与子类中的属性命名冲突, ...

  6. Python类中的__dict__属性

    __dict__属性是一个字典(dict),它包含了该对象所有的属性. 下面用pytorch中的torchvision.models作比喻: [input]:import torchvision.mo ...

  7. 用《饿狼传说》中的不知火舞女神来验证python模块中imshow()和show()的区别

    1. imshow()只负责对图像进行处理和显示格式,但不会显示在屏幕上. 2. show()函数将图片显示到屏幕上. 代码实现 import matplotlib.pyplot as plt # 用 ...

  8. python中模块的__all__属性解读

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

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

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

最新文章

  1. [case19]聊聊eureka的TaskDispatcher
  2. [JZOJ P1291] [DP]添加括号
  3. bootstrap-进度条
  4. group plot simplest approach in matlab
  5. 贪心只能过样例 loj515
  6. python如何运行源文件_Python如何运行
  7. MySQL2种多实例部署方式总结
  8. 微信小游戏出台最严健康游戏管理 未成年游戏时间金额受限制
  9. 随想录(lua源码学习)
  10. 正则匹配字符串有则替换无则添加;用正则实现添加和替换字符串,原字符串中包含某字段就替换(覆盖),不包含某字段就添加!
  11. 上海电机学院c语言,上海电机学院第1章_C语言概述.ppt
  12. opencv中很有趣的仿射变换(Affine Transformation)
  13. python网站开发实例 flask_python-flask框架web服务接口开发实例
  14. 异常重试_面试题:如何基于 dubbo 进行服务治理、服务降级、失败重试?
  15. yota3无第三方recovery root方法
  16. 奥维天地图解决办法!如何申请个人天地图API密钥?
  17. excel使用教程_excel表格中页面布局下的分页符使用教程,以及使用excel宏vba批量插入分页符...
  18. 基于C#.NET对战卡牌游戏-《混战》游戏开发
  19. 软件测试人员需不需要懂代码
  20. win7下修改php.ini的配置路径

热门文章

  1. ASP.NET 2.0 中的资源与本地化
  2. 分页原理+软件架构师
  3. php定时删除文件夹下文件(清理缓存文件)
  4. 如何将.sof转换成.jic
  5. Effective C++ 条款44
  6. win7乱码 字符集解决方案
  7. HTML5主要新增标签
  8. 推荐10款非常有用的 Ajax 插件
  9. 兄弟连区块链教程Fabric1.0源代码分析configupdate处理通道配置更新
  10. 「每天一道面试题」String和StringBuilder、StringBuffer的区别