python Learning Day 19

Module
Python module (Module1), Python is a file, ending .py, containing Python Python object definitions and statements.
Module allows you to logically organize your Python code.
The relevant code is assigned to a module can make your code easier to use, more understandable.
Modules can be defined functions, variables, and classes, where the module can also contain executable code.
1; the statement import
module introducing
the module well defined, we can use the import statement to the introduction of the module, the syntax is as follows:
import module1 [, Module2 [, ... moduleN]]
For example, to reference the module math, can be used where the file beginning import math introduced. When calling the function math module, so you must quote:
Module name function name
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 interpreter will carry out a search. As you want to import module support.py, need to command at the top of the script:
2; ... Import statement from
Python's from statement lets you import a specified part from the module into the current namespace. The syntax is as follows:
from modname Import NAME1 [, NAME2 [, ... nameN]]
3; * Import from ... statement
to all the contents of a module are all imported into the current namespace is also possible, simply use the following statement:
from modname Import *
This provides an easy way to import all items in a module. However, this statement should not be too much to use.
For example, we want to introduce a one-time math module in all things, the statement is as follows:
from math Import *
4; search path

When you import a module, Python parser search order for the location of the module are:

1, the current directory

2, if not in the current directory, Python is a shell variable PYTHONPATH search in each directory.

3, if not find, Python will look at the default path. In UNIX, the default path is typically / usr / local / lib / python /.
Module search path is stored in the variable sys.path system module. Variable contains the current directory, PYTHONPATH and the default directory is determined by the installation process.
PYTHONPATH variable
as an environment variable, PYTHONPATH by a number of directories contained in a list of components. PYTHONPATH shell variable PATH syntax and the same.
5; namespace and scope

The object is to have the matching variable name (identifier). Namespace is a variable that contains the name of their (key) and their respective objects have (value) of the dictionary.

A Python expression can access local and global namespace namespace variable. If a local variable and a global variable the same name, the local variable will override the global variable.

Each function has its own namespace. The method as classes scoping rules and functions normally.

Python intelligently guess a variable is local or global, it is assumed that any variable assignment within the function are local.

Therefore, if the function to give global variable assignment, you must use the global statement.

global VarName expressions will tell Python, VarName is a global variable, so Python will not find this variable in the local namespace.

For example, we define a variable Money in the global namespace. Let us Money assigned to variables within a function, then Python will assume Money is a local variable. However, we did not declare a local variable Money before the visit, the result is a UnboundLocalError of error. Cancellation global statement before the Notes will be able to solve this problem.
6; dir () function

dir () function is a row sorted list of strings, the content is a module been defined name.

Receiving the returned list of all modules, variables and functions defined in a module.
Globals () and about locals () function

Depending on local calls, globals () and locals () function can be used to return the global and local namespace name.

If the locals call inside the function (), it returns all names that can be accessed within the function where.

If you call globals () inside the function returns all the function names in the global energy access.

The return type of the function are two dictionary. So we can name keys () function removal.

reload () function

When a module is introduced into a script, top part of the code module is executed only once.

So, if you want to re-execute the code module in the top section, you can use reload () function. The import function module re-imported before.
7; Python package

Package file is a hierarchical directory structure, which defines a Python application environment and the like by the sub-packet on the modules and sub-packets and sub-packets thereof.
In simple terms, the package is a folder, but the folder must exist under the init .py file, the contents of the file can be empty. the init .py the current folder is used to identify a package.

Consider a directory under package_runoob runoob1.py, runoob2.py, the init .py file, test.py test the code calls the package,Here Insert Picture Description

Published 19 original articles · won praise 0 · Views 766

Guess you like

Origin blog.csdn.net/weixin_46269994/article/details/104640519