1, the module is a core concept python program architecture
2, each ending with an extension to the python py are a source file module
3, the module name is also an identifier, you need to follow the naming rules for identifiers: ① begin with a letter, underscore, not digital numbers ② ③ must be unique keyword
4, the global variable definition in the module, function, class are supplied to the external tool used directly
5, the development of principles: Each module should be able to be imported
Import module 1.1 manner
Write module imports should be unified at the top of the code, it is easier to detect a conflict, in this order: built-in modules, expansion modules, custom modules
If two modules, a function of the same name exists, then the module after importing function will overwrite function first introduced
Once a conflict is found, which can be used as a key tool to an alias
1) import module name: one-time module all the tools Import All
an import module name, the module under 2 ----- not recommended, does not meet the regulatory requirements for coding EP8 python
----- when importing the module, it should be introduced into each separate line
Use as specified module alias: import module module name as an alias
Module alias should be in line with the large hump nomenclature
You can improve the compatibility of the code
2) from a certain import module name --- tool from a module, the introduction portion of the tool
After the module name does not need to import . , May be used as a tool module
import * from module name from the module can import all the tools, and can be used directly after the import tool.
But not recommended, because the function of the same name does not have any tips, troubleshooting problems is not good.
With all the methods used by __all __ = [ "variable name 1", "variable name 2"] may be introduced only all variables in the list
1.2 search order module
python interpreter when you import the module, the module will search the current directory name specified file, if there is direct import. If not, then search the system directory.
Named file, not the file system of the same name and the module
python each module has a built-in attribute __file__ can view the full path to the module
1.3 __name__ property
__name__ properties can be done, only the code test module is operated in the module under test, and will not be executed when introduced
python __name__ property is a built-in property, a string recorded
If other files are being imported, __ name__ is the module name, if it is currently executing program, __ name__ is __main__
In the bottom of the code #
def main():
pass
if __name__ == "__main__": # whether to perform under the code based on judgment __name__
main()
1.4 pyc file (compiled compiled)
In interpreting the python source, the first source code, the compiler generates a binary code byte, the byte code and then the process will generate machine code that can be recognized CPU
pyc file is converted by a python interpreter source code module bytecode, the bytecode is stored python such as a startup speed optimization
With the byte code file module, the next program is running, if the source code has not been modified since the last save byte code, Python will load .pyc files compiled skip this step and
When recompile python, it will automatically check the time stamp source files and bytecode files, and if you modify the source code, the next time the program is run, the bytecode will automatically re-created