08_python function

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 returnstatement to return to. returnBehind 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 key CTRL + Qyou 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 need import(to import) this 模块.
  • Each of the extension to pythe end of the Pythonsource 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 .pyfiles is a module.
  • Using custom module as long as importthe 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.pycis: .

  • .cpython-35Representative explained the version number.
  • .pycBytecode files compiled on behalf of, cis compilean 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 .pycfile.
  • If the program has been modified, then the python interpreter will first re-generate a new .pycfile, and then load the file.

Guess you like

Origin www.cnblogs.com/lasnitch/p/11568717.html