table of Contents
1. Initial module
1.1 Introduction module
- First we look at what the module is in front of it we wrote a Python file some function, after we finish these functions can only be used in your own Python file, when the function we want to be able to make their own written across files use, Python provides a way, that's the module.
1.2 Module Definition
- Some definitions stored in a file, for a number of scripts or interactive compiler instance , this file is called
模块
. So we wrote some of the above file containing Python is actually a function of the modules. - Module is a file that contains all the functions and variables you define which file extensions
.py
. Module can be referenced and used by other modules, implemented using performance function you write in a different Python files. And we usually use the Python standard library, in fact, the calling module.
Examples module 1.3
Example 1.3.1:
# !/user/bin/python # coding=utf-8 import math __author__ = "zjw" def helloModule(): print "Hello Module!" if __name__ == '__main__': print math.pi helloModule()
Output:
3.14159265359 Hello Module!
analysis:
- Comment the first two rows of two standard annotations, which can make the first line of the file directly run on Unix / Linux / Mac, only these three operating systems valid (Reference: Python program # / usr / bin / python! explanation ), while the second is to use a standard file itself indicates .py UTF-8 encoding.
- The following
import
statement is to import the module. Here imported the math module, we have a variablemath
pointing to the module, take advantage ofmath
this variable, we can access all the features of the module. - In the following
__author__
keyword is used to indicate the author, where we can be signed, marked on his pen name. - In the following we define a
helloModule()
function, followed by amain
function, respectively calledmath
the value of pi module, and write their own functionshelloModule()
.
2. Import module
2.1 Case Study (key part, look at the focus)
Direct look at an example:
This is a project I built some Python and Python packages and files it contains. Note: Here each module has a module function hello, hello output function has only one function.
Here are five different call
test01
packagemodule
method, aremain.py
implemented module, the code module:# !user/bin/env python # coding=utf-8 import test01.module01 # 方法1 from test01 import module02 # 方法2 from test01.module03 import hello03 # 方法3 import test01.module04 as m4 # 方法4 from test01.module05 import hello05 as m5 # 方法5 __author__ = "zjw" if __name__ == '__main__': print "Hello world!" test01.module01.hello01() # 方法1 module02.hello02() # 方法2 hello03() # 方法3 m4.hello04() # 方法4 m5() # 方法5
Output:
Hello world!
Hello module01!
Hello module02!
Hello module03!
Hello module04!
Hello module05!Note: Here we tragically above packages and modules correlation diagram of the project, understand the code.
- According to my fumbling, discovered a problem, is the path to the module in Python is from the root directory of the project began to look for the appropriate module . So, suppose we want to call when calling other modules in different packages in the same project, when we set up the module path is to start looking at the root, that is where the root directory is
main.py
located. - Method 1: we use that
import
statement directly into the appropriate module. After the import module calls, the use we need to bring all of its import statements, accompanied by an appropriate method to call. - Method 2:
from...import...
sentence, from where the heel of a package name, which the latter requires a module name import. When you call directly模块名+方法名
to. - Method 3: At the same
from...import...
statement, what difference will it make? We see here from the heel to the module name is a specific location, and then after import directly into a method. You can use the direct method. Of course, we can also usefrom 模块名 import *
statement to import all methods module. This provides an easy way to import all items in a module. However, this statement should not be too much to use. - Method 4: The method is complementary to 1,
import...as...
statements, Import introduced after the relevant module, the module AS rename. When we use the following directly rename identifiers plus method name. - Method 5: is a supplement to the method 3,
from...import...as...
statements, we can direct method calls rename identifiers can be used directly after renaming use.
- According to my fumbling, discovered a problem, is the path to the module in Python is from the root directory of the project began to look for the appropriate module . So, suppose we want to call when calling other modules in different packages in the same project, when we set up the module path is to start looking at the root, that is where the root directory is
2.2 The import statement explained
2.2.1 import statement
grammar:
import module1[, module2[, module3...]]
When the interpreter encounters the import statement, if the module will be imported in the current search path.
Search path is a list of all directories that an editor will carry out a search. And when we import is, Python editor will be followed from these directories to search for modules introduced by Python's search path.
Note: a module will only be imported once, no matter how many times you perform the import, which prevents the same module to perform again and again.
Now suppose we should always be used when a function in a module with the import imported, calling to bring the module name, it may be a bit tedious, this time can be solved using the following method:
>>> import math >>> pi = math.pi >>> pi 3.141592653589793
- Analysis: Here import the Python module built-in
math
module, the following assumptions have been used topi
this value, but each call must be usedmath.pi
to get, so it is somewhat cumbersome, so we can bemath.pi
assigned topi
this variable, then pick down in this functionpi
can be obtained function values, and become much more convenient.
- Analysis: Here import the Python module built-in
2.2.2 from ... import ... statement
grammar:
from module_name import name1[, name2...]
Here again to import the math library pi an example to illustrate:
>>> from math import pi >>> pi 3.141592653589793
- Analysis: you can see when we import the function with the statement, when we call the function only need
pi
to. This statement is not the wholemath
module into the current namespace, it will only function to import some of them, and when you can just use the name of the function call.
- Analysis: you can see when we import the function with the statement, when we call the function only need
2.2.3 from ... import * statement
grammar:
from module_name import *
This syntax can be used for all functions in one module all of the current introduced into the namespace.
This may seem very simple, when such a declaration should not be too much use, because the phenomenon is likely to cause the same name identifier.
2.2.4 as statements
grammar:
import module as new_name
from module_name import name as new_name
Two examples to illustrate:
Example 2.2.4.1:
>>> import math as m >>> m.pi 3.141592653589793
Example 2.2.4.2:
>>> from math import pi as p >>> p 3.141592653589793
Analysis: you can see
as
with the end of the statement is take the name, like assigning a function to import than to the other variables, can simplify the difficulty of the following call.
2.3 dir () function
Python built-in
dir()
function can find all the function names defined within the module.Example 2.3.1:
>>> import math >>> dir(math) ['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
- Analysis: When we import a module, forget the name of a function to call, we can use the dir function to help us remember the name of the function needed. Haha, is not it silly cock, but it feels quite practical.
Example 2.3.2:
>>> import math >>> dir() # 得到一个当前模块中定义的属性列表 ['__builtins__', '__doc__', '__name__', '__package__', 'math'] >>> del math # 删除了导入的math模块 >>> dir() # 重新用dir函数查看,发现math模块已经删除 ['__builtins__', '__doc__', '__name__', '__package__']
- Analysis: If no argument is given, then the dir () function will set out the names of all currently defined.
3. Scope
3.1 Introduction
- Like java in the private and public these keywords can limit the scope of variables and methods in Python is how to achieve it?
- In a summary module, we might define a lot of functions and variables, but some functions and variables we want to give other people with, but some want to use within their own module, this time related to the concept of scope.
- Python, is through
_
to achieve different scopes prefix.
3.2 explain
- public type, can be external calls:
- Normal function and variable names are disclosed, may be directly referenced externally, such as
abc
,count
,PI
and the like.
- Normal function and variable names are disclosed, may be directly referenced externally, such as
- Special type:
- Like
__xxx__
when variables such special variables that can be referenced directly, but for special purposes, such as__author__
,__name__
is a special variable, our own variables generally do not use this variable.
- Like
- private type, can only be their own internal module call:
- Similar
_xxx
and__xxx
such function or variable is private and can not be directly referenced, for example_abc
,__count
,__PI
and the like.
- Similar
- When we define the function name, the function does not require an external reference in its entirety is defined as private, only you need to reference external function was defined as public. So that we can be hidden with a private internal logic functions together, you do not care about the internal logic of private function inside the external function call, which is a very useful code encapsulation and abstraction methods.