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> 导入方式并没有影响,仍然可以从外部导入。
(完)
python模块中的__all__属性的更多相关文章
- python学习笔记013——模块中的私有属性
1 私有属性的使用方式 在python中,没有类似private之类的关键字来声明私有方法或属性.若要声明其私有属性,语法规则为: 属性前加双下划线,属性后不加(双)下划线,如将属性name私有化,则 ...
- python模块中的特殊变量
37.模块的特殊变量: 显示模块中的变量 import s1 print(vars(s1)) 1.__doc__:打印注释信息. #!/usr/bin/env python # _ ...
- 嵌入Python系列 | 调用Python模块中无参数函数
开发环境 Python版本:3.6.4 (32-bit) 编辑器:Visual Studio Code C++环境:Visual Studio 2013 需求说明 在用VS2013编写的Win32程序 ...
- 嵌入Python | 调用Python模块中无参数的函数
开发环境 Python版本:3.6.4 (32-bit) 编辑器:Visual Studio Code C++环境:Visual Studio 2013 需求说明 在用VS2013编写的Win32程序 ...
- Python - 模块中的"if __name__ == '__main__':"
1.1 如果导入的模块除了定义函数之外还中有可以执行代码,那么Python解释器在导入这个模块时就会执行这些代码. module1.py: def foo(): print('module 1') f ...
- Python开发【第一篇】Python模块中特殊变量
模块中特殊变量 生产环境中,常用的就是__name__和__file__ __doc__ __package__ __cached__ __name__ __file__ 一. __doc__ #获 ...
- python 模块中__all__作用
test.py文件开头写上__all__=[func1,func2] 当其他文件导入 from test import * 只会导出"[func1,func2]"里面的,其他调用 ...
- 说说Python多线程中的daemon属性方法
大家看多线程部分的时候肯定看到过daemon这个属性,当我在百度了一圈后也没发现有比较好的解释(或者大家对这个解释都非常清楚),于是自己通过代码和官方介绍了解它,进行了一些总结 给大家一些参考. 首先 ...
- python 模块中的 __init__.py __main__.py
python中文件夹想作为一个模块被引用,则在文件夹内必须要包含 __init__.py 文件,即使此文件为空. 如果此模块想要运行则必须要包含 __main__.py 文件.接下来说下两个文件起到的 ...
随机推荐
- 使用Tornado实现http代理
0x00 http代理 http代理的用处非常多,市面上也有公开的代理,可是有时候为了工作须要,比方分析应用层流量.做数据訪问控制.甚至做监控等等.Tornado提供了一些非常方便的环境和API,我们 ...
- 【转】使用Python中HTTPParser模块进行简单的html解析
http://www.cnblogs.com/coser/archive/2012/01/09/2317076.html
- Elasticsearch学习系列之介绍安装
前言 关于ELK搭建的问题,或许你还有些模糊,其实你把我视频里讲的知识点串联起来就明白了.搭建ELK环境,看下面我说的: 首先,先把ES集群搭建起来,建议用CentOS6.5 64位的linux系统, ...
- Eclipse中的Web项目自己主动部署到Tomcat以及怎样在Eclipse中使用My Eclipseproject
我是一个新手学习Java,servlet和Jsp. 痛苦的是我时候一个.net程序猿,习惯了微软的VS IDE一切都是封装好的.傻瓜式的使用, 不须要关心内部实现. 悲催的是我看到资料都是My Ecl ...
- (linux shell)第一章--小试牛刀(下)
文章来源: (linux shell)第一章--小试牛刀(下) 1.6 数组和关联数组 1.6.1 预备知识 Bash同一时候支持普通数组和关联数组.普通数组仅仅能使用整数作为数组索引,而关联数组能够 ...
- react 项目实战(三)表单验证
我们需要记录每一个字段当前的有效状态,有效时隐藏错误信息,无效时显示错误信息. 而这个有效/无效,可以在表单值改变的时候进行判断. 我们对/src/pages/UserAdd.js进行修改: 首先修改 ...
- redis 主从 及集群
一.redis 主从架构 搭建redis 主从 (可以用一台主机,也可以两台主机) 环境准备: 一台服务器:192.168.206.6 操作系统:CentOS7.5 redis 版本: redis ...
- Centos7 防火墙firewalld配置
开启80端口 firewall-cmd --zone=public --add-port=80/tcp --permanent 出现success表明添加成功 移除某个端口 firewall-cmd ...
- YTU 2898: C-Z型变换
2898: C-Z型变换 时间限制: 1 Sec 内存限制: 128 MB 提交: 53 解决: 15 题目描述 让我们来玩个Z型变换的游戏,游戏的规则如下: 给你一个字符串,将它以Z字型的形状不 ...
- forceStopPackage与killBackgroundProcesses方法
最近了解一键清理功能,需要实现强制关闭进程的功能.下面介绍下killBackgroundProcesses()方法和forceStopPackage()方法. killBackgroundProces ...