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 文件.接下来说下两个文件起到的 ...
随机推荐
- Codeforces Round #247 (Div. 2) B
B. Shower Line time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- myloader恢复mysql数据库演示样例
mydumper是针对mysql数据库备份的一个轻量级第三方的开源工具.备份方式为逻辑备份.它支持多线程.备份速度远高于原生态的mysqldump以及众多优异特性.与其相配套的恢复工具则是mylo ...
- 【Linux多线程】同步与互斥的区别
同步与互斥这两个概念经常被混淆,所以在这里说一下它们的区别. 一.同步与互斥的区别 1. 同步 同步,又称直接制约关系,是指多个线程(或进程)为了合作完成任务,必须严格按照规定的 某种先后次序来运行. ...
- Python输入输出及其他
print用法 print会输出一个\n,也就是换行符,这样光标移动到了下一行行首,接着输出,之前已经通过stdout输出的东西依旧保留,而且保证我们在下面看到最新的输出结果.回车 \r 本义是光标重 ...
- tesnorflow conv deconv,padding
1.padding test input = tf.placeholder(tf.float32, shape=(1,2, 2,1)) simpleconv=slim.conv2d(input,1,[ ...
- 【bzoj2743】[HEOI2012]采花
树状数组 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring& ...
- HNOI模拟 Day3.23
一.拓扑(top)[ 题目描述]:给你一个有向二分图,求他的拓扑序列的个数.[ 输入]:第一行两个数 N,M,表示点数和边数.接下来 M 行每行两个数 a,b,表示 a 向 b 有一条有向边.[ 输出 ...
- ios20--xib2
故事板控制器: // // ViewController.m // 03-通过xib自定义商品的View #import "ViewController.h" #import &q ...
- 【Dairy】2016.10.23 观火&中彩记
...................... 就第一条可以! 观火10分钟,长郡附近老房子起火...
- Groonga开源搜索引擎——列存储做聚合,没有内建分布式,分片和副本是随mysql或者postgreSQL作为存储引擎由MySQL自身来做分片和副本的
1. Characteristics of Groonga ppt:http://mroonga.org/publication/presentation/groonga-mysqluc2011.pd ...