[Base] python module package &

A module

  1, the so-called modules, refers to certain features of a good package .py file. If you want to use a module directly into the line.
  2, random.py module name is the file name (does not contain a suffix)
  3, naming the module name, follow the naming rules for identifiers. Variables, function names, class names, module name

Second, the role of the module

  1, program development files are large, are placed in the same file, management and maintenance is very inconvenient. Split into multiple modules to manage, easy to maintain.

  2, the module can increase the rate of reuse program.

Third, the classification module

  1, programmer package
  2, python own module; Random, Time, datetime, SYS, .... OS
  . 3, third-party modules. Written by someone else, very powerful, we are in the process of program development, the direct use of
    their own to install python interpreter to be able to use, cmd -> pip install module

Fourth, the import module

  1, import the module name
  2, import module name as an alias [Note]: surnamed import, you can not use the original name of the module
  3, import the module name, the module name 2, .... into more than a one-time module (not recommended)
  . 4, from import ... import module from the module variable, function, class, ...
  . 5, from AS alias module import ... [NOTE]: import from the alias can not be reuse original name
  6, from module import the object 1, the object 2, the object 3, .... disposable module into the same number of variables, classes, functions, ...
  . 7, from module to module import * All variables, classes, functions, all imported into the current module (not recommended)

  Module import module will be executed again introduced

Five, __ name__ 

  1, if the current .py file is executed, the __name__ the module __main__ =
  2, if the current file is executed as the import module, the import module of the __name__ module name =

  Since the module name is not defined the same name that comes with python module name, or the import will be your current custom module

Six, pyc temporary file (for custom modules concerned)

  1, in order to increase the speed of the load module, python interpreter will be imported pyc file cache module compiled in __pycache__ directory to be introduced later again, in fact, this temporary file is imported pyc of
  2, python interpreter intelligently believe that only the incoming import file is a file that needs to be reused, so only generates a temporary file pyc files to be imported

Seven, __ all__

  When the content import * import module from the module, may be provided by the content import property __all__
  __all__ a property list, the list element is a string element

Eight, package

  1, Features

    python package file must have a __init__.py file

    A folder, there may be multiple .py files in this folder,
    a package can have many modules

  2, introduced into a module package (if the package in the current path)
    1. Import module name package name.    
      [Used]: package name module name function name ()

    2. import module name from the name of the package
      from the package name. Import function module name name

  3. from the package name import *
    premise: __init__.py __all__ need to assign attribute of the file, assign the imported content to allow modules
        __all__ of type list, the list element is a string element
           __all__ = [ "module name "," demo2 "," test_demo "]

Guess you like

Origin www.cnblogs.com/Tree0108/p/12110205.html