definition : The usual architecture of a Python program is to divide a program into a collection of source code files and a way to connect those parts together.
The program architecture of Python can be expressed as:
A python program is a system of modules. It has a top-level file (which can be run after startup) and multiple module files (to import the tool library).
Note: Standard library modules: Python's own utility modules, also known as standard link libraries.
There are about 200 modules in the aggregate, including the common program design tasks that are not related to the platform: Operating system interface, object persistence, text matching mode, network and Internet scripting, GUI construction, etc. Note: These tools are not part of the Python language,
However, you can import the appropriate modules to use in any case where standard Python is installed.
Second, the module
concept : A module is the highest-level organizational unit in Python that encapsulates program code and data for reuse. In fact, every Python file ending with the extension. py is a module.
three roles of the module :
(1) code reuse;
(2) the partition of the System namespace (the module can be understood as the encapsulation of the variable name, that is, the module is the namespace);
(3) realize shared services and data.
programs and modules : In Python, a program is constructed as a main, top-level file, with 0 or more supported files, which can be called modules (top-level files can also be used as modules, but generally not as modules).
top-level file : Contains the main control flow of the program: the file that needs to be run to launch the app.
module Files : Can be seen as a repository of tools (i.e., full of tools), which are used to collect the components used by the top-level files (or wherever possible).
top-level files and module files : Top-level files use the tools defined in the module files, and the tools defined by other modules are used for these modules as well.
execution Environment for modules : modules contain variables, functions, classes, and other modules (if imported), and functions have their own local variables. Describes the situation within the module and the interaction with other modules, that is, the execution environment of the module:
visible : Modules can be imported, but modules will also import and use other modules, which can be written in Python or other languages (e.g., C language).
Iii. Import (Importing)
Concept : A file can be imported into a module (file) to read the contents of this module, that is, import in essence, is to load another file in one file, and be able to read the contents of that file. Content within a module can be used by the outside world through such attributes (object . attribute).
Import is the focus of the program structure in Python.
1, import a module four ways
- Import x: Imports Module X and creates a reference to the module in the current namespace (NAMESAPCE). You can use: X. The name reference defines the property in module X.
- From X Import * : Imports module X, and in the current namespace, creates a reference to all public objects in the module (the name does not begin with __). That is, you can use the normal name (directly name) to refer to the properties in module X, but x itself is not defined and cannot be used with x.name. And if the namespace has a name definition that originally has the same name, it will be replaced by the new name.
- From x import a, B, C: Imports Module X, and creates a reference to the given object for the module in the current namespace.
- x = __import__ (' x '): similar to (1) Import x, the difference is that the mode display specifies that X is a variable in the current namespace. Use the same method.
2. What do python do when you import a module?
With import a moudle, first, the Python interpreter checks the module registry (sys.moudles) section to see if it was previously imported and if it already exists in sys.modules (that is, registered). You can use the currently existing Module object. If the sys.modules does not already exist, then:
(1) Create a new, empty module object (essentially a dictionary);
(2) Insert the Module object in the Sys.modules dictionary;
(3) load the object that corresponds to the module code (you can compile it first if you want).
Then in the new module namespace, execute the module code object. All variables specified by the code can be referenced by the module object.
Note : The above steps will only be performed when the module is first executed. After this, when the same module is imported, these steps are skipped, and only the module objects that are loaded in memory are extracted. This is a deliberate design result . Because importing (finding files-compiling them into bytecode-running code) is a very expensive operation, and each program cannot be run more than once. If you want Python to run the file again in the same session (without stopping and restarting the session), you need to call the built-in reload (overloaded) function (which returns a Python module object).
3. Import Search Path Order
(1) The program's home directory : The directory where the program (top-level) file resides (sometimes different from the current working directory (the directory where the Launcher is located)).
(2) PYTHONPATH (environment variable) directory
(3) Standard link library directory
(4) the contents of any. pth file (if present): Locate the file in the installation directory and add the desired directory as a line.
The above four components are combined into sys.path, which preserves the actual configuration of the module search path on the machine and can be viewed by printing the built-in Sys.path list. When importing, Python will search from left to right for each directory in the list, until the corresponding module is found.
Where the search path (1) and (3) are automatically defined by the system, and (2) (4) can be used to expand the path, thereby adding their own source code directory.
Such as:
#!/usr/bin/env Python3#-*-coding:utf-8-*-ImportWebBrowser as WebImport TimeImportOSImportRandom Count= Random.randint (5,7) J=0 whilej<=count:i=0 whileI<=8: Web.open_new_tab ('https://www.baidu.com')#URL to fill in your ownI=i+1Time.sleep (0.8) Else: Os.system ('taskkill/f/im Chrome.exe') Print(J,'Time Webbrower closed') J=j+1
A brief analysis of Python program architecture