1.python custom function
(1) function defines the python
The basic syntax for defining a python function without parameters:
def 函数名():
函数中封装的代码
……
note:
- The function name should see the name to know Italian.
- Naming function name should comply with the identifier naming rules.
- Defined functions indentation very strict.
(2) function of the parameters and return values python
python in the formal and actual participation of other high-level language similar. But it is not the type of function, if the function returns a value, use direct return
statement to return to. return
Behind the statement will not be executed.
(3) the function call python
By calling the function name, 函数名(参数列表)
similar to other high-level language. No arguments directly to the default.
(4) python function documentation comments
- In development, if you want to add comments to the function, should be
定义函数
below, the use连续的三对引号
of the function definition is described. - In the
函数调用
position, use the shortcut keyCTRL + Q
you can view the function description information. - When a function definition is recommended, with the use of other code separated by two blank lines. Because the function is a function module.
(5) python nested function
Like other high-level language.
2.python function modules
(1) python module Overview
Python module is a core concept of the program architecture
模块
Similar to java in包
order to use the tools in this package, you needimport
(to import) this模块
.- Each of the extension to
py
the end of thePython
source code files are a模块
. - As defined in the module
全局变量
,函数
all modules can provide tools to the outside world directly.
(2) using the function module python
- Import module
- Use
模块名.变量
or模块名.函数
method, using the variable or function defined in the module.
Defining and using (3) custom module
- Write your own
.py
files is a module. - Using custom module as long as
import
the module can. - Name the module (source files) should be in line with the rules for identifiers.
- If the module name does not conform to the rules for identifiers, it will import fails.
(4) The benefits of using python modules
- Facilitate code reuse. (Similar to the java package.)
The step of the source program is explained 3.python
Step (1) python interpreter in
- Compiling the source code to generate byte code file.
- Byte code files are processed to generate machine code file for execution cpu.
(2) the location and name of the generated byte code files
In the automatically generated __pycache__ python directory file generated bytecode. Its name 文件名.cpython-35.pyc
is: .
.cpython-35
Representative explained the version number..pyc
Bytecode files compiled on behalf of,c
iscompile
an abbreviation.- If the program had been run, and do not make changes, then the python interpreter will be loaded directly original generated when you run the program again
.pyc
file. - If the program has been modified, then the python interpreter will first re-generate a new
.pyc
file, and then load the file.