A summary of the modules in Python, as well as the import of packages

Source: Internet
Author: User

How modules are imported:

  Module concept: A. py file is called a module

How to import functions in the module:

Mode one: Import module name

      When used: module name. function name ()

Mode two: From Module name Import function name

      When used: function name ()

Mode three: From Module name import *

      When used: function name ()

Mode four: From Module name import function name as TT (custom) Note that the original function name is invalid

      When used: TT ()

You can define a variable __all__ in the module:

    The effect of using __all__: what function name is written in the following [], what is imported when importing using the From Module name import * __all__ If none of this variable will be imported (__all__ is limited to the From module name import * this import method)

Add __all__ Example:

  1 __all__ = ["Demo"]  2 def demo ():  3   4     Print ("Demo")  5   6 def demo1 ():  7     print (" Demo1 ")

  Operation Result:

In [1]: From TT import *in [2]: Demo () Demoin [3]: Demo1 ()-------------------------------------------------------------- ---------Nameerror                             

Example without __all__:

1 def demo ():  2   3     print ("demo")  4       5 def demo1 ():  6     print ("Demo1")

Operation Result:

In [1]: From TT import *in [2]: Demo () Demoin [3]: Demo1 () demo1

How packages are imported

  Concept: Contains several py files .... Inside creating a __init__ file is the package

Import the functions in the package, and how the modules are:

Mode one: From Package name Import module name

  when used: module name. function name ()

Mode two: From package name. module Name Import function name

  When used: function name ()

Mode three: Import package name. Module Name

   When using the package name. Module name. function name ()

Mode four: From Package name import *

  The premise is: write the __all__ variable in the __init__.py file (write mode with the module import) variable, which module is written into which module to import, do not import nothing

  When used: module name. function name ()

Mode five: Import package name

  The premise is that the __init__.py file in the package is written from . Import module name __init__.py which module can be used in this way

When used: module name. function name ()

Mode six: Import package name

  Python2 in the __init__.py file can be written in the Import module name through the import package name can be imported __init__.py file written in the module name (this method is limited to Python2)

When used: module name. function name ()

Description of the. pyc file:

the import produces a. PYc bytecode file, This file is the first time that the Python interpreter will pre-interpret the imported module as a byte-code file, the next time you import the Python interpreter will not do a pre-interpretation, but directly with the. PYc file, so that does not have to explain every time the import operation, save time, When modifying the contents of a module file, the Python interpreter will determine if the module has been modified according to the modified time of the. pyc file and Module if the module has been modified for a later date than the. pyc file. The Python interpreter interprets the module as a. pyc file.

Modules in Python, and summary of imports of packages

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.