python 单下划线和双下划线

2018-06-23 13:27:41来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

 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()

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:python 列表(list)常用操作

下一篇:python中sorted方法和列表的sort方法使用详解