Learning python - module

  • # What is a module?
    • A collection of already written functions #
    • # Others written functions, variables, methods, in a file (the file that we can be used directly)
    • # import os,re,sys,time
  • # How to write a module
    • # When creating .py file, give him a name in line with the variable name naming the file name, the name is the module name
  • Import Module #
    • # import time
    • # How to use time module name
      • # time.time()
    • # Import namespace
      • #import modules and the current file in a different namespace
    • # Module may be repeated if introduced
      • # Can not
    • # Determine how this module has been imported over
      • # print(sys.modules)
    • What # when you import the module happened?
      • # Find this module
      • # Determine whether the module is imported before, if not to import it belongs to create a namespace of this module, so the module name to point to this space
      • # Execution module
      • # Aliases to the module, since the module can only use this alias after using the alias reference variables
    • # import time as t,re as r
      • Introducing a plurality of modules #
      • # Specification recommends introducing order: introduction built, and then introduced into the expansion module, and finally introduced into the custom module
    • # from time import gmtime
      • # How to use from import
      • # Need to use a file from which the name, put the name came Import
      • #From import process in the implementation of this document will still be imported
      • # Import will come only with who
      • Namespaces and modules of the current file #
      • #From import process the import of what happened
        • # 1. Find module
        • # 2. Determines whether introduced through
        • # 3. If you had not been imported
          • # Create a namespace belong to this module
            • # This file is executed
          • # To the variable you want to import
          • # Variables you want to import to create a reference to the variables you want to import
      • # Import multiple names
        • #from time import time as t,gmtime as g
      • #from time import *
        • # Import time in more than a content module
      • Relations #from time import * and the __all__
        • * #__all__ constraint variables introduced in the module,
  • Cycle # 1. Module reference ***
    • It does not allow circular references between modules #
  • # 2. The module is loaded with modifications **
    • # Has been imported modules have changes, it will not be perceived
    • # So, we need to modify the module is running a program perceived, can only restart the program
  • # 3. ***** use the module as a script
    • #if __name__ == '__main__':
      • # Code (written in this code inside the file as a script execution only when will be executed)
    • # Execute a file of way
      • # Cmd, pycharm execution: Direct executable file - way to script execution
      • # Import the file
      • Py files are #
        • # Direct run this file, this file is the script
        • # Import the file, this file is the module,
    • # When a file py
      • # As a script when: the ability to provide a separate function, to complete the self-interaction
      • # As a module of the time: the importer can be called this function, not self-interaction
    • # A file variable __name__
      • # When this file as a script execution __name__ == '__ main__'
      • # When this file as imported modules __name__ == 'module name'
  • # 4. Module search path
    • # And the file is executed as a script with the module directory can be imported directly into
    • # In addition to the module under other paths again when the need to import a list of their own change sys.path

Guess you like

Origin www.cnblogs.com/bilx/p/11366758.html