Previous article: The contents of the Python tutorial (Chapter 5) are itemized
Python3 Engineer Certification Basic Exam As a countermeasure, this is a personal memo that summarizes the contents of the Python tutorial (book) in easy-to-memorize bullet points.
Python Tutorial: https://docs.python.org/ja/3/tutorial/ Chapter 6: https://docs.python.org/ja/3/tutorial/modules.html Books: https://www.oreilly.co.jp/books/9784873117539/
--Python3 Engineer Certification Basic Exam Score ―― 2/40 questions (5.0%) ☆ ★★★★ (importance: small) --Theme
-** Module ** is a file that contains Python definitions and statements. --The file name of the module is "module name.py" --A set of variables that can be accessed from interactive mode or top-level scripts is called ** main module . --The module name is set in the global variable ** \ _ \ _ name__ ** in the module. --What is defined in a module can be ** imported ** into another module or main module. - import fibo * # fibo module is imported - fibo.fib (1000) * #Call the fib () function of the fibo module -* fib = fibo.fib * # Assign the fib () function of the fibo module to a local variable -* print (fibo. \ _ \ _ Name__) * #Print module name'fibo'
--When you put an executable statement in the module, it will be executed at the following timing. --When you run the module as a script. --When called by an import statement from another module (when the module name is encountered for the first time). --Each module has a private symbol table. --You can also import other modules from a module. --The imported module name is placed in the global symbol table on the imported side. -** from ** allows you to import only the specific name of the module. -* from fibo import fib, fib2 * # Import only the fib () and fib2 () functions of the fibo module -* from fibo import ** # Import names other than underscore _ --import * is rarely used as it often reduces readability.
--The execution command of the Python module is as follows. -* python fibo.py Arguments * # Execution command --When you run the Python module, \ _ \ _ name__ will contain "\ _ \ _ main__". -* if \ _ \ _ name__ == "\ _ \ _ main__": * # Execute the code below only when the script is executed
--The interpreter searches for imported modules in the following order.
--Python caches compiled modules in the ** \ _ \ _ pycache__ directory . --The cache name is module.version name.pyc - Version name ** is the compiled file format code, including the Python version number. --Compiled modules are platform independent. --Python compares the source code modification date and cache version to determine if recompilation is necessary. --The cache is not checked in the following cases. --When the module is loaded directly from the command line. --In this case, recompilation is always performed and the result is not saved. --When the source file of the module does not exist. --To support sourceless distribution, the following is required: --Put the compiled module in the source directory. --Do not put the source file itself.
--Python comes with a library of standard modules. --The module configuration is determined by the settings at the time of installation. --There are also interpreter built-in and platform-dependent modules. --The interpreter embedded module is provided for performance needs and to provide access to OS primitives. --Platform-dependent modules include the winreg module for Windows. -** sys module ** is included in all Python interpreters. --sys.ps1 ... Primary prompt string definition ('>>>') --sys.ps2 ... Secondary prompt string definition ('...') --These two are defined only when the interpreter is in interactive mode. --sys.path ... A string list that specifies the module search path for the interpreter --The following directories are the initial values of sys.path. --Directory with input script --Current directory if no file name is specified --Environment variable PYTHONPATH --Built-in default
-** The dir () function ** returns the name defined by the module in a sorted list of strings. --If you specify a module name as an argument, the name defined by that module is returned. --With no arguments, returns the name defined at the current level. --Names include variables, functions, module names, etc. --Import builtins module to return built-in function name or variable name. -* dir (builtins) * ... Get the built-in name list
-** Package ** builds Python module names using "dot separated module names". --The substance of the package is a hierarchical structure of directories and a set of modules (.py files) in it. --The directories that make up the package must contain ** \ _ \ _ init__.py files . -Use of \ _ \ _ init__.py file: --Execution of package initialization code - \ _ \ _ all__ Variable ** (described later) set
--The behavior when import * is specified for the package is as follows. -If the \ _ \ _ all__ variable is defined in the \ _ \ _ init \ _ \ . Py file, import the submodule defined by the \ _ \ _ all_ variable. -If the \ _ \ _ all__ variable is not defined, the following will be imported. --Import of the corresponding package --All names defined in the package body -Names defined in the \ _ \ _ init \ _ \ _. Py file and explicitly loaded submodules
--If the package consists of subpackages, there are two ways to refer to the sister package. -** Absolute import ** ... from module name (absolute path) import name -** Relative import ** ... ftom module name (relative path) import name --Use. (Current module hierarchy) or .. (parent hierarchy) as relative paths. --Use absolute import from modules that may be used as the main module. --Because the name is always "\ _ \ _ main \ _ \ _".
-** \ _ \ _ path \ _ \ _ variable ** contains a list that includes the directory where \ _ \ _ init \ _ \ _. Py exists by default. --Initialization is done before running \ _ \ _ init \ _ \ _. Py. --By rewriting this variable, you can influence the search for modules and subpackages contained in the package.
Next article: A bulleted list of the contents of the Python tutorial (Chapter 7) (under construction)
Recommended Posts