Python\ Module 4

Source: Internet
Author: User

1 Definition of module and Package 1.1 module

What is a module

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

Why use a module

After exiting the Python interpreter and then re-entering, the previously defined function or variable will be lost, so we usually write the program in the file so that it can be persisted and executed in Python test.py when needed, and test.py is called a script.

We usually divide the process into documents, 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.

1.2 How to use the module 1.2.1 Import

A module can contain executable statements and definitions of functions that are intended to initialize modules that are executed 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, Python is optimized by loading the module name into memory after the first import, followed by an import statement that only adds a reference to the module object that has loaded the large memory, and does not re-execute the statements within the module.

#spam. Pyimport spam# Executes the spam.py code only at the first import, where the effect is to print only once of course the other top-level code is also executed, except that it doesn't show the effect import spamimport spamimport spam >>from the spam.py

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

Each module is a separate namespace, defined in this module of the function, it is the namespace of the module as the global namespace, so 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

money=1222# Global definition variable moneyfrom spam import money,read1# call spam module print (money) >>from the spam.py1000# Print variable money in spam module

Bind money in a namespace

#test. Pyimport spamdef resd1 ():    print ("---->") spam.read1 () #调用spam的属性 >>from the spam.pyspam->read1- >money 1000

Import modules can be imported multiple at the same time, separated by commas

Import Sys,os,re

Assigning a value in the namespace Read1,read1 function cannot be called

 

Summary: Three things to do when importing modules for the first time

(1) Create a new namespace for the source file to hold the name defined in the source file

(2) based on the created namespace execution module

(3) Create a name (such as spam) to refer to the namespace, file name. operation of the name, whichever is the file that created the name

Module alias use

Import spam as sm# re-name the Spam module smprint (sm.money) #相当于print (Spam.money) >>from the spam.py1000

The way you alias an imported module is useful for writing extensible code, assuming that two modules define the same function, but with different input formats. You can write code to selectively select the Read module

if File_format = = ' xml ':     import XmlReader as reader elif File_format = = ' csv ':     import csvreader as Reader Data=rea Der.read_date (filename)

 

1.2.2 from Import

Comparing import spam, the namespace of the source file is brought into the current namespace and must be spam. The way of the name

The FROM statement is equivalent to import, and a new namespace is created, directly using the name to

From spam import money,read1read1 () >>from the Spam.pyspam->read1->money 1000

If you currently have rename Read1, then there will be an overlay effect

From spam import Money,read1def read1 ():    print ("--->") read1 () >>from the spam.py--->

Aliases can also be used in the FROM statement

From spam import read1 as read

You can also import multiple lines

From spam import (Read1,                 read2, Money                 )

From spam import *

Import all names in spam that are not preceded by an underscore to the current location.

Cons: Do not know what name to import, it is likely to overwrite the previously defined name

Extremely readable.

can use __all__ to control *

No problem when executing read1

Execute read2 times wrong

Python does not support reloading or uninstalling previously imported modules

1.3 Module as script execution

The calling module will not be used

1.4 Search Path of the module

The order in which the modules are found is: Modules loaded in memory-à modules included in the-àsys.path path of the built-in modules

Note: the custom module name should not be the same as the system built-in module

1.5 Compiling Python files

Import module only

From import

The PYc file module is loaded to increase speed

Python\ Module 4

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.