python 模块的导入(__all__)

 1 # -*- coding:utf-8 -*-
 2 # author:平手友梨奈ii
 3 # e-mail:1353593259@qq.com
 4 # datetime:2019/10/9 0009 14:07
 5 # filename:import1.py
 6 # software: PyCharm
 7 """
 8     python中可以通过from XXX import *导入另外一个module中的成员属性、方法、类
 9     若在模块中定义了__all__,则只有在__all__中定义的属性、方法、类可以被导入,否则导入所有的【公共】属性、方法、类
10     --------------------------------------------------------------------------------------------------------
11     在python中,约定以单下划线开头的为protected,以双下划线开头的为private
12     在java中,各种类的权限如下:
13     -----------------------------------------------------------------
14                \ 本类 \ 本包中的其他类 \ 其他包中的子类 \ 其他包中的类
15     public     \  √  \      √       \       √       \     √
16     -----------------------------------------------------------------
17     protected  \  √  \      √       \       √       \
18     -----------------------------------------------------------------
19     default    \  √  \      √       \                \
20     -----------------------------------------------------------------
21     private    \  √  \               \                \
22     -----------------------------------------------------------------
23 """
24 
25 __all__ = [
26     'function1',
27     'function2',
28     '_function',
29     '__function'
30 ]
31 
32 def function1(x):
33     print('公有函数function1被调用', x)
34 
35 
36 def function2(x):
37     print('公有函数function2被调用', x)
38 
39 
40 def _function(x):
41     print('protected函数被调用', x)
42 
43 
44 def __function(x):
45     print('private函数被调用', x)
1 from import1 import *
2 
3 function1(x='techi')
4 function2(x='techi')
5 _function(x='techi')
6 __function(x='techi')
公有函数function1被调用 techi
公有函数function2被调用 techi
protected函数被调用 techi
private函数被调用 techi

 

posted @ 2019-10-09 14:41  平手友梨奈ii  阅读(494)  评论(0编辑  收藏  举报