python模块中的__all__属性
转自:http://blog.csdn.net/sxingming/article/details/52903377
python模块中的__all__属性,可用于模块导入时限制,如:
from module import *
此时被导入模块若定义了__all__属性,则只有__all__内指定的属性、方法、类可被导入。
若没定义,则导入模块内的所有公有属性,方法和类 。
- # kk.py
- class A():
- def __init__(self,name,age):
- self.name=name
- self.age=age
- class B():
- def __init__(self,name,id):
- self.name=name
- self.id=id
- def func():
- print 'func() is called!'
- def func1():
- print 'func1() is called!'
- #test_kk.py
- from kk import * #由于kk.py中没有定义__all__属性,所以导入了kk.py中所有的公有属性、方法、类
- a=A('python','24')
- print a.name,a.age
- b=B('python',123456)
- print b.name,b.id
- func()
- func1()
运行结果:
python 24
python 123456
func() is called!
func1() is called!
- #kk.py
- __all__=('A','func') #在别的模块中,导入该模块时,只能导入__all__中的变量,方法和类
- class A():
- def __init__(self,name,age):
- self.name=name
- self.age=age
- class B():
- def __init__(self,name,id):
- self.name=name
- self.id=id
- def func():
- print 'func() is called!'
- def func1():
- print 'func1() is called!'
- #test_kk.py
- from kk import * #kk.py中定义了__all__属性,只能导入__all__中定义的属性,方法和类
- a=A('python','24')
- print a.name,a.age
- func()
- #func1() #NameError: name 'func1' is not defined
- #b=B('python',123456) #NameError: name 'B' is not defined
运行结果:
python 24
func() is called!
- #kk.py
- def func(): #模块中的public方法
- print 'func() is called!'
- def _func(): #模块中的protected方法
- print '_func() is called!'
- def __func():#模块中的private方法
- print '__func() is called!'
- #test_kk.py
- from kk import * #这种方式只能导入公有的属性,方法或类【无法导入以单下划线开头(protected)或以双下划线开头(private)的属性,方法或类】
- func()
- #_func() #NameError: name '_func' is not defined
- #__func() #NameError: name '__func' is not defined
运行结果:
func() is called!
- __all__=('func','__func','_A') #放入__all__中所有属性均可导入,即使是以下划线开头
- class _A():
- def __init__(self,name):
- self.name=name
- def func():
- print 'func() is called!'
- def func1():
- print 'func1() is called!'
- def _func():
- print '_func() is called!'
- def __func():
- print '__func() is called!'
- from kk import *
- func()
- #func1() #func1不在__all__中,无法导入 NameError: name 'func1' is not defined
- #_func() #_func不在__all__中,无法导入 NameError: name '_func' is not defined
- __func() #__func在__all__中,可以导入
- a=_A('python') #_A在__all__中,可以导入
- print a.name
运行结果:
func() is called!
__func() is called!
python
- #kk.py
- def func():
- print 'func() is called!'
- def _func():
- print '_func() is called!'
- def __func():
- print '__func() is called!'
- #test_kk.py
- from kk import func,_func,__func #可以通过这种方式导入public,protected,private
- func()
- _func() #NameError: name '_func' is not defined
- __func() #NameError: name '__func' is not defined
运行结果:
func() is called!
_func() is called!
__func() is called!
- #kk.py
- def func():
- print 'func() is called!'
- def _func():
- print '_func() is called!'
- def __func():
- print '__func() is called!'
- #test_kk.py
- import kk #也可以通过这种方式导入public,protected,private
- kk.func()
- kk._func() #NameError: name '_func' is not defined
- kk.__func() #NameError: name '__func' is not defined
运行结果:
func() is called!
_func() is called!
__func() is called!
- #kk.py
- import sys
- __all__ = ["func"] # 排除了 'sys'
- def func():
- print 'func() is called!'
- #test_kk.py
- from kk import *
- #print sys.path #NameError: name 'sys' is not defined
- 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__属性相关推荐
- Python培训教程分享:Python模块如何导入__all__属性?
本期小编为大家带来的Python培训教程是关于"Python模块如何导入__all__属性?"的内容,后面在工作中是会遇到Python模块这个工作内容的,Python模块的开头通常 ...
- Matplotlib模块中没有verbose属性
Matplotlib模块中没有verbose属性 Matplotlib是Python中广泛使用的数据可视化库,支持绘制各种图表.在使用Matplotlib时,有些用户可能会遇到"module ...
- Python类中的私有属性和私有方法
Python类中的私有属性和私有方法 Python是面向对象的语言,在Python里一切皆对象,所以大部分情况下,我们都是通过类的方式来编程. 但是Python中的属性和方法在类的外部默认是可以访问的 ...
- python 模块命名空间_如何在python模块中执行导入而不污染其命名空间?
我正在开发一个用于处理一些科学数据的Python包.在其他模块和包中有多个经常使用的类和函数,包括numpy,我几乎需要在包的任何模块中定义的每个函数. Pythonic的处理方式是什么?我已经考虑过 ...
- python面向对象中的私有属性和私有化方法
xx: 公有变量 (公有) _x: 单前置下划线,私有化属性或方法,from somemodule import *禁止导入,类对象和子类可以访问 __xx:双前置下划线,避免与子类中的属性命名冲突, ...
- Python类中的__dict__属性
__dict__属性是一个字典(dict),它包含了该对象所有的属性. 下面用pytorch中的torchvision.models作比喻: [input]:import torchvision.mo ...
- 用《饿狼传说》中的不知火舞女神来验证python模块中imshow()和show()的区别
1. imshow()只负责对图像进行处理和显示格式,但不会显示在屏幕上. 2. show()函数将图片显示到屏幕上. 代码实现 import matplotlib.pyplot as plt # 用 ...
- python中模块的__all__属性解读
python模块中的__all__属性,可用于模块导入时限制,如: from module import * 此时被导入模块若定义了__all__属性,则只有__all__内指定的属性.方法.类可被导 ...
- python init main_python 模块中的 __init__.py __main__.py
python中文件夹想作为一个模块被引用,则在文件夹内必须要包含 __init__.py 文件,即使此文件为空. 如果此模块想要运行则必须要包含 __main__.py 文件.接下来说下两个文件起到的 ...
最新文章
- [case19]聊聊eureka的TaskDispatcher
- [JZOJ P1291] [DP]添加括号
- bootstrap-进度条
- group plot simplest approach in matlab
- 贪心只能过样例 loj515
- python如何运行源文件_Python如何运行
- MySQL2种多实例部署方式总结
- 微信小游戏出台最严健康游戏管理 未成年游戏时间金额受限制
- 随想录(lua源码学习)
- 正则匹配字符串有则替换无则添加;用正则实现添加和替换字符串,原字符串中包含某字段就替换(覆盖),不包含某字段就添加!
- 上海电机学院c语言,上海电机学院第1章_C语言概述.ppt
- opencv中很有趣的仿射变换(Affine Transformation)
- python网站开发实例 flask_python-flask框架web服务接口开发实例
- 异常重试_面试题:如何基于 dubbo 进行服务治理、服务降级、失败重试?
- yota3无第三方recovery root方法
- 奥维天地图解决办法!如何申请个人天地图API密钥?
- excel使用教程_excel表格中页面布局下的分页符使用教程,以及使用excel宏vba批量插入分页符...
- 基于C#.NET对战卡牌游戏-《混战》游戏开发
- 软件测试人员需不需要懂代码
- win7下修改php.ini的配置路径