python script variable usage __all__

Python is a py file is a module, "__ all__" variable is a special variable, it may also be present in py file in __init__.py package.

1, when used in an ordinary module, which represents a property of the module may be introduced to allow the other module,

     such as: a global variable, function, class. Below, test1.py and the main.py

     test1.py

1
2
3
4
5
6
7
__all__ = [ "test" ]
 
def  test():
     print ( '----test-----' )
      
def  test1():
     print ( '----test1----' )

 
   main.py

1
2
3
4
5
6
7
8
from  test1  import  *
      
def  main():
     test()
      
     #test1()
      
main()

 
Two files in the same directory.

At this time, when executed python main.py results are as follows:

 

 


But if liberalization comment main.py, as follows:

 

 


So __all__ variables in the module is intended to limit or specify the module can be imported into other functions, classes, global variables, if specified, then only those specified may be imported, you can not specify a default is all introducing, except of course, should the private property.

2, in the package, __init__.py

sound / effects / __ init__.py added __all__ = [ "echo", " surround", "reverse"]

it will at * is from sound.effects import, contains the above three modules. When __init__.py is empty, the package just introduced is not introduced into the module.

__init__.py can perform some initialization content, such as:

. Import from test1 test1 introduced into the current directory module

from .. import test on the induction level directory under test module

because at first performs the import package __init__.py file

original: https: //blog.csdn.net/chuan_day/article/details/79694319 

Guess you like

Origin www.cnblogs.com/wisir/p/12526401.html