Python learning _day27_ modules and packages

Source: Internet
Author: User

First, the module

A module is a file that contains the Python definition and declaration, and the filename is the suffix of the module name plus the. Py.

1, the Call of the module

  Call Syntax:import ... or from ... import ...

  invocation: the same module will not be re-imported (import statement can be used anywhere in the program, and for the same module import multiple times, in order to prevent you from repeating the import, Python optimization means: The first import after the module name loaded into memory, Subsequent import statements only add a reference to a Module object that has already loaded large memory, does not re-execute the statements within the module, and the order in which the modules are imported: Built-in modules, extension modules that need to be installed, modules that you write, rules for importing modules: Do not import multiple modules on the same line

2. Module namespace

Each module is a separate namespace, defined in this module of the function, the namespace of the module as the global namespace, so that when we write our own module, we do not have to worry about our own module in the definition of global variables will be imported, and the user's global variables conflict.

 #  my_module module  money  =1000def   Read1 ():  print  ( " my_module->read1->money   " ,money)  def   Read2 ():  print  ("  my_module->read2 calling Read1   " ) read1 ()  def   change ():  global   Money money  =0 

Example 1:money does not conflict with My_module.money

Import My_modulemoney=10print(My_module.money)                 # output:

Example 2:read1 () does not conflict with My_module.read1 ()

Import My_module def read1 ():     Print ('========') my_module.read1 ()                       # output Result: My_module->read1->money

3. Module renaming

The way you alias an imported module is useful for writing extensible code. Statement: "From module name Import name as rename" or "Import name as rename"

Application Example: There are two kinds of modules MySQL and Oracle, both of which contain the Sqlparse () function, and choose different sqlparse () functions according to the user's input.

#mysql.pydefsqlparse ():Print('From MySQL sqlparse')#oracle.pydefsqlparse ():Print('From Oracle Sqlparse')#test.pyDb_type=input ('>>:')ifDb_type = ='MySQL':    ImportMySQL as DBelifDb_type = ='Oracle':    ImportOracle as Dbdb.sqlparse ()

4. From...import ... Related

Comparing import My_module, the namespace ' my_module ' of the source file is brought into the current namespace, and must be used as a my_module. Name, and the from statement is equivalent to import, and a new namespace is created, but the My_ The name in the module is imported directly into the current namespace, and the name can be used directly in the current namespace.

 from Import Read1 def read1 ():     Print ('aaaaa') read1 ()

Python learning _day27_ modules and 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.