码迷,mamicode.com
首页 > 编程语言 > 详细

python 单下划线和双下划线

时间:2018-01-13 22:19:32      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:import   私有   方式   port   nbsp   from   gpo   pass   blog   

 1 underline.py
 2 __all__ = [_underline_variable, __underline_variable, _underline_func,
 3            __underline_func, _underline_class, __underline_class]
 4 
 5 
 6 _underline_variable = 100
 7 __underline_variable = 200
 8 
 9 
10 def _underline_func():
11     pass
12 
13 def __underline_func():
14     pass
15 
16 class _underline_class():
17     pass
18 
19 class __underline_class():
20     pass
21 
22 
23 class Base():
24     def _underline(self):
25         pass
26 
27     def __underline(self):  # 私有
28         pass
29 
30 
31 class Derived(Base):
32     pass
33 
34 if __name__ == __main__:
35     base = Base()
36     base._underline()
37     #base.__underline()
38     base._Base__underline()
39     
40     derived = Derived()
41     derived._underline()
42     #derived._Derived__underline()
43     derived._Base__underline()
 1 test.py
 2 # 1 用import方式导入
 3 import underline
 4 
 5 print(underline._underline_variable)
 6 print(underline.__underline_variable)
 7 underline._underline_func()
 8 underline.__underline_func()
 9 test1 = underline._underline_class()
10 test2 = underline.__underline_class()
11 
12 print(* * 20)
13 
14 
15 # 2 没有定义__all__属性,用from ... import *方式导入
16 #from underline import *
17 #print(_underline_variable)
18 #print(__underline_variable)
19 #_underline_func()
20 #__underline_func()
21 #test1 = _underline_class()
22 #test2 = __underline_class()
23 
24 
25 # 3 定义__all__属性,用from ... import *方式导入
26 from underline import *
27 print(_underline_variable)
28 print(__underline_variable)
29 _underline_func()
30 __underline_func()
31 test1 = _underline_class()
32 test2 = __underline_class()

 

python 单下划线和双下划线

标签:import   私有   方式   port   nbsp   from   gpo   pass   blog   

原文地址:https://www.cnblogs.com/gundan/p/8280486.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!