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