Python module File structure code details, python module code details
This article focuses on the file structure of the Python module.
Python file structure (full text)
#/usr/bin/env python "this is a test module" import sys import os debug = True class FooClass (object): "Foo class" pass def test(): "test function" foo = FooClass() if debugL print 'ran test()' if __name__ == '__main__' test()
File structure (branch Introduction)
(1) starting line (specifying the version of Python used)
#/usr/bin/env python
(2) module document (document string, if it is expanded with three single quotes)
"this is a test module"
(3) module Import
import sys import os
(4) (global) variable definition
debug = True
(5) class definition (if any)
Class FooClass (object): "Foo class" pass (6) function Definition (if any) def test (): "test function" foo = FooClass () if debugL print 'ran test ()'
7) Main Program
if __name__ == '__main__' test()
Description
Main Program
- This part of code is executed no matter whether the current module is imported or directly run as a script.
Note: All modules have the ability to execute code
- The high-level Python Statement (without indentation) is executed when the module is imported.
- Proper practice: Except for the code that really needs to be executed, all functional code is built through the function. Therefore:
Write a large number of top-level executable code only in the main program module
Only a small number of top-level Execution Code should exist in the imported module.
_ Name _ indicates how the module should be loaded (so as to implement self-check of the module)
- If the module is imported, the value __name _ is the module name.
- If the module is executed directly, the value of __name _ is _ main __
Summary
The above is all the details about the file structure code of the Python module. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!