A variable that begins with an underscore (such as _x):
Under certain circumstances, it can be underlined that the signature of the variable name (such as _x) prevents the client from copying the name of the module when importing it using the FROM * statement, but the underscore is not a ' private ' declaration: You can still see and modify such variables using other import forms, such as using import
__all__ (statement)
It is also possible to assign a string list of variable names to the variable __all__ at the top of the module to achieve a hidden effect similar to the _x naming convention; Using this feature, the from* statement will only copy these variable names that are listed in the __all__ list
__all__ is the name of the variable that is to be copied, and the variable name that is not copied when _x. Python will first look for a list of __all__ in the module (if you define __ALL__, only the variable names in __all__ will be copied, not the variable names with no underscore at the beginning), and if there is no definition, the from * will copy all variable names with no single underscore at the beginning
The __all__ list is valid only in the form of the FROM * statement, not a private statement
Example:
#test. py
Key= ' Got '
_nooutpute= ' lllllllllllllll '
Error= ' FASDFSA '
Code= ' FSADFSA '
Decode= ' 324324 '
__all__=[' ERROR ', ' Code ', ' decode ']
Print {' already ': (lambda:2+2),
' Got ':(lambda:2*4),
' One ': (lambda:2**4)
}[key] ()
#_X__all__. py
From Test import *
Print (Error)
Print (key)
Note the point:
You can see that the __all__ must be a variable name and must be assigned before referencing it.
#test. PY Change Test __all__
Error= ' FASDFSA '
Code= ' FSADFSA '
Decode= ' 324324 '
__all__=[' ERROR ', ' Code ', ' Decode ', ' testname ']
#_x__all__. py
From Test import *
Print (Error)
Print (testname)
Python _x __all__ Simulation Imports ' private ' variables and their points of attention and examples