Python-based modules and packages

Source: Internet
Author: User

First, the module

1. What is a 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.


2 Why do I use modules?
If you quit the Python interpreter and then re-enter, then the functions or variables you defined previously will be lost, so we usually write the program to a file so that it can be persisted and executed in Python test.py when needed, when test.py is called a scripting script.
With the development of the program, more and more functions, in order to facilitate management, we usually divide the program into a file, so that the structure of the program is clearer and easier to manage. At this point, we can not only use these files as scripts to execute, but also as a module to import into other modules, to achieve the reuse of functionality.


3. How do I use the module?

Three ways to carve out namespaces:
Classes, Functions, modules

3.1 Import

Sample file: spam.py, file name spam.py, module name spam

#spam.pyPrint('From the spam.py') Money=1000defread1 ():Print('Spam->read1->money', 1000)defread2 ():Print('Spam->read2 Calling Read') Read1 ()defChange ():Global Money Money=0

(1) A module can contain executable statements and definitions of functions that are intended to initialize modules that execute only when the module name is first encountered when importing an import statement (the import statement can be used anywhere in the program and is imported multiple times for the same module. To prevent you from repeating the import, the Python optimization means that the module name is loaded into memory after the first import, and the subsequent import statement only adds a reference to the module object that has loaded the large memory, does not re-execute the statements within the module, as follows

# test.py Import # The spam.py code is executed only on the first import, where the explicit effect is to print only once ' from the spam.py ', and of course the other top-level code is executed, except that it does not show the effect. Import spam Import spam Import spam " " execution Result: from the spam.py " "

We can find the module that is currently loaded from Sys.module, Sys.module is a dictionary that contains the mapping of the module name to the module object, which determines whether the import module needs to be re-imported.

(2) 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 definition in our own module global variables will be imported, and the user's global variables conflict

 #   Test one: Money does not conflict with Spam.money  #  test.py  import   spam money  =10print   (Spam.money)  "   Execution result: From the spam.py1000   
 #   Test two: Read1 does not conflict with Spam.read1  #  test.py  import   spam  def   Read1 ():  print  ( " ========   '  
# Test Three: Global variables to perform the Spam.change () operation money is still in spam # test.py Import Spammoney=1spam.change ()print(money)' execution result: from the Spam.py1  " "

(3) Summary: When the first import module spam will do three things:

Python-based 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.