Module bis

One, two ways of introducing module

  1, import Import: When using the internal module name, module name needs to add embellishment.

    ① advantages: Because of the module name prefix, so it will not be repeated within a namespace using another name.

    ② Cons: Because of the module name prefix, so the name seemed lengthy.

  2, from ... import: If using from declared namespace in which the name of the module, and then use that name without adding the module name prefix.

    ① the same point import import process: open name space module === "execution module file ===" in the name of the module into the module namespace.

    ② different from the import of the import process: the module will produce the same name within the namespace namespace procedures used, pointing to the corresponding memory address.

    ③ advantage: because you do not add the module name prefix, so the name appear to be more streamlined.

    ④ Disadvantages: Because there is no module name prefix, so the name at the memory address can be modified using the program covered within.

    ⑤ one-way import: Like you can import, using from ... import name 1, name 2, 3 ... the name of the form to import multiple names on a single line, but is not recommended for this use.

    ⑥ Import all: You can use the form from ... import * import module once all the names. All the names in the module can be used __all__ specified range.

    ⑦ aliases: from ... import whose real name as an alias.

Second, the priority module search path: introducing retrieval module is valid.

  1, the first step: to find the code in memory, just started py file, only the built-in memory module namespace.

    ① mechanism once and for all: After the first import module, within the memory during program execution using the application, the module name space will remain, even if the middle of unbound modules and module namespace, module name space is not because the reference count of 0 is recovered.

    ②sys.modules: View loaded into memory namespace.

  2, the second part: the environment variable sys.path order of file folder path stored sequentially introduced lookup module exists.

    ①sys.path: is a stored list of multiple folder paths, the first of which is the use of the file folder where the folder.

    ②sys.path.append (): the path to the folder in which the file module filled environment variables, you can retrieve this list and find when you import the module.

Third, the module type of package: Package file is a file folder containing __init__.py, essentially in the form of another module, the module can be used as introduced.

  1, the process of introducing the package:

    ① produce a namespace package.

    ② run the package __init__.py file folder under the name generated during operation of the namespace thrown into the package.

    ③ package produced using the namespace program name, the name of the package to namespaces.

  2, unified environment variables: all modules are imported execute files need to refer to sys.path, are subject to sys.path executable file.

  3, the package is introduced:

    ① introducing the package has two points from ... import and import in two ways.

    ② Either way, when you import a statement to the parent folder, ie the left point must be a package that must be a file folder, otherwise they will be in the form of illegal and error, can not contain the module name, if that is imported. Name form. It can be concluded, not specific to import Import name with up to module name to the specific name, the name required from ... import form.

    ③ use the name after the import is no such limitation, a name can be referenced by prefixing orientation.

    ④ can be clipped together on the parent folder in the parent file, but can not cross file is not declared in the environment variable executable file in the folder.

    ⑤ import package is the nature of the import file __init__.py, nature namespace package is __init__ namespace, the name of the package is generated when importing into the space. I.e. introduction and use are blocked intermediate .__ init__. Format.

    You may also be introduced using single row ⑥ package into multiple names, aliases, and introducing all these methods.

 

 

Guess you like

Origin www.cnblogs.com/caoyu080202201/p/12590548.html