Module allows you to logically organize your Python code.
Assigning the relevant code into a module to make your code easier to use, more understandable.
Python modules are objects with random name attribute is used to bind or reference.
Simply put, the module is a Python code to save the file. Modules can define functions, classes and variables. Module can contain executable code.
Called aname module in Python code in general can be found in a file called aname.py. The following example is a simple module support.py.
def print_func( par ): print "Hello : ", par return
Want to use Python source file, simply perform another import statement in the source file, the syntax is as follows:
import module1[, module2[,... moduleN]
When the interpreter encounters the import statement, if the module will be imported in the current search path.
The search path is an interpreter will first search for a list of all directories. As you want to import module support.py, you need to put the command at the top of the script:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 导入模块 import support # 现在可以调用模块里包含的函数了 support.print_func("Zara")
Examples of the above output:
Hello : Zara
A module will only be imported once, no matter how many times you execute import. This prevents the import module is executed over and over again.
From ... import statement
Python allows you to import a statement from a specified portion from the module into the current namespace. The syntax is as follows:
from modname import name1[, name2[, ... nameN]]
For example, to import the module fib fibonacci function, use the following statement:
from fib import fibonacci
This statement does not fib the entire module into the current namespace, it will only fib Lane fibonacci introduced into a single global symbol table implementation of this declaration module.
From ... import * statement
All the contents of a module are all into the current namespace are possible, simply use the following statement:
from modname import *
This provides an easy way to import a module in all projects. However, this statement should not be too much to use.
When you import a module, Python parser module position search order is:
- Current directory
- If not in the current directory, Python will search each directory in the shell variable PYTHONPATH.
- If you can not find, Python will look at the default path. Under UNIX, the default path is usually / usr / local / lib / python /.
Module search path is stored in sys.path variable system modules. Variable contains the current directory, PYTHONPATH and the default installation directory by the decision-making process.
As an environment variable, PYTHONPATH by the mounted in a list of many of the directories. PYTHONPATH shell variable PATH syntax and the same.
On Windows systems, the typical PYTHONPATH as follows:
On UNIX systems, the typical PYTHONPATH as follows:
Namespaces and scopes
Variable is the name of the object has a matching (identifier). Namespace is a variable that contains the name of their (key) and their respective objects are (value) of dictionaries.
A Python expression can access local and global namespace namespace variable. If a local variable with the same name as a global variable, the local variable will override the global variable.
Each function has its own namespace. Scope rules as class methods and functions normally.
Python intelligently guess a variable is local or global, it is assumed that any variable within a function assignment is local.
Therefore, if you give a global variable assignments in a function, you must use the global statement.
global VarName expression tells Python, VarName is a global variable, so Python does not find this variable in the local namespace.
For example, we define a variable money in the global namespace. We then within the function assigned to the variable money, then Python will assume that money is a local variable. However, we did not declare a local variable before accessing the money, the result is that there will be a UnboundLocalError mistake. Cancellation global statement comments can solve this problem.
#!/usr/bin/python # -*- coding: UTF-8 -*- Money = 2000 def AddMoney(): # 想改正代码就取消以下注释: # global Money Money = Money + 1 print Money AddMoney() print Money
dir () function
dir () function is a list of strings row good sequence, the content is a module defined name.
Returns a list of all the modules to accommodate, variables and functions defined in a module. A simple example is as follows:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 导入内置math模块 import math content = dir(math) print content;
Examples of the above output:
['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']
Here, special string variable __name__ point name of the module, __ file__ point to Import file name of the module.
globals () and locals () function
Depending on local calls, globals () and locals () function can be used to return the global and local namespace name.
If you call the locals inside the function (), returns can access all the functions in the name.
If you call globals () inside the function returns all the function names in the global energy access.
Both functions return type is the dictionary. So the name we use keys () function removal.
reload () function
When a module is introduced into a script module top part of the code is executed only once.
So, if you want to re-execute the module in the top part of the code, you can use the reload () function. This function will be re-imported before the imported module. The syntax is as follows:
Here, module_name name of the module to be placed directly, instead of a string. For example, you want to override the hello module, as follows:
Package is a hierarchical directory structure, which defines a child package by the modules and sub-packages, and subpackages under other components of the Python application environment.
Consider a pots.py file in the Phone directory. This file has the following source code:
#!/usr/bin/python # -*- coding: UTF-8 -*- def Pots(): print "I'm Pots Phone"
Similarly, we have two different functions to save the file:
- Phone / Isdn.py containing function Isdn ()
- Phone / G3.py containing function G3 ()
Now, create a file __init__.py in Phone Directory:
- Phone / __ init__.py
When you import the Phone, to be able to use all the functions you need to use an explicit import statements in __init__.py years, as follows:
from Pots import Pots from Isdn import Isdn from G3 import G3
When you add the code to __init__.py, imported Phone package when these classes are all available on the.
#!/usr/bin/python # -*- coding: UTF-8 -*- # 导入 Phone 包 import Phone Phone.Pots() Phone.Isdn() Phone.G3()
Examples of the above output:
I'm Pots Phone I'm 3G Phone I'm ISDN Phone
As, for example, we have only one function in each file, but in fact you can put a lot of functions. You can also define a Python class in these files, and then build a package for these classes.