python basis - function Detailed

python basis - function Detailed

First, what is the function

函数:就是一套定义好的流程,输入数据得到结果。函数之间可能相互独立,也可能一个函数的输入是另一个函数的输出,也可能一个函数的内部调用另一个函数。
		或者说是将具有独立功能的代码块组织成为一个整体,使其具有特殊功能的代码集

Second, the function returns

Use function can enhance the reusability of code, written procedures to improve efficiency

Third, the definition of the function

python a function structure:

def 函数名(参数1,参数2,参数3):
	函数体第一行
	函数体第二行
	……
	函数体第N行
	return 返回值
   一个函数可以有参数,也可以没有参数。如果没有参数函数名后面为一对括号,如果函数有	
参数参数可以有一个,也可以有很多个参数,可以以任何数据类型,函数参数,甚至可以是另
一个函数。
函数可以没有return语句,有一个return语句,也可以有多个return语句。

The following three cases are equivalent:
1. There is no return statement.
After 2.Return statement without any value.
3.Return None

	在函数中可以使用return将里面的结果返回。出来。哪一代运行到了return,那么函数就会结束
return后面的代码都不会被执行。
	在一个Python工程中,应保证每个函数名称唯一,函数体就是这个函数需要执行的一系列操作
操作可能只有一行。也可能有很多行。
	函数只做一件事Python编码规范建议一个函数的函数体不超过20行代码,如果超过,说明这个函数做了不止一件事,就应该把这个函数拆分成为更小的函数。

Fourth, the scope and function namespaces

1. Scope (scope): the entry into force of the variable region

In Python There are two scopes
of the global scope :( similar to the windows in the path environment variable)

  • Global scope created when the program is executed, destroyed at the end of program execution
  • All functions are outside the area of ​​global scope
  • In the definition of variables of global scope, are part of the global variables, global variables may be accessed at any point in the program

Function scope :

  • Function scope is created when the function is called, destroyed at the end of call
  • Each call to the function will generate a new function scope
  • In the definition of the function scope variables are local variables, which can only be accessed within the function

Find variables (similar to the lookup windows in the environment variable)

  • When we use variables, it will give priority to looking at the current scope of the variable, if there is used, if not then continue to look for on a scope, if there is used, if still not then continue to go on a role Looking domain, and so on until you find the global scope, is still not found, an exception is thrown
    NameError: name 'a' is not defined
    Here Insert Picture Description

2. namespace (namespace)

  • Namespace : variable storage locations, each variable need to be stored to a specified namespace which
    each will have a scope that a namespace corresponding
    global namespace used to store global variables. Function namespace is used to save a function of variable
    namespace is actually a dictionary, a dictionary designed to store a variable

locals () is used to get the current namespace scope
if you call the locals in the global domain () Gets the global namespace, if the locals call the action function domain () function to obtain the namespace, returns a dictionary, You can add or delete variables dictionary of the modifications.

Fifth, call the function

Serial data between functions can run, first by a processing function, but also a process in another function, can also be nested run a function calls another function which, of course, also function in which the function is defined, which are advanced function.

1. recursive function:

The whole idea is that a big problem into one small problem, until the problem can not break down, go solve the problem of
two elements recursive function
1. Baseline conditions
- minimum problem can be decomposed into question, and when to meet the baseline when the condition is not performing a recursive
2. recursion conditions
- conditions will continue to decompose problems
recursive loop, and the like, basically can replace each other,
circulation is easier to write, read up a little difficult
recursively to write and difficult, but easy to read

2. Higher-order functions:

As a function of the received parameters or function return values as a function of higher order is a function of
when we use a function as a parameter, the specified code is actually transmitted into the objective function

Fifth, the function parameters

Parameter 1:
the definition parameter may specify a default value for the parameter
specify a default value after the user if the default value is passed parameter has no effect
if the user is not passed, the default value will be effective
2. arguments transmission mode
position parameter
position parameter corresponding to the real position is copied to the reference parameter corresponding to position
the first argument is assigned to the first parameter, the second argument is assigned to the second parameter. . .
fn (1, 2, 3)

Keyword parameters
keyword parameters, the order parameter may not be defined to transfer, directly to the transfer parameters according to the parameter name
Fn (= B. 1, C = 2, A =. 3)
Print ( 'Hello', End = '' )
positional parameters and keyword parameters can be mixed
when mixing keywords and location parameters, parameters must be written to a position in front of
fn (1, c = 30)
variable-length parameters :

在定义函数时,可以在形参前边加上一个*,这样这个形参将会获取到所有的实参
它将会将所有的实参保存到一个元组中
*a会接受所有的位置实参,并且会将这些实参统一保存到一个元组中(装包)
 带星号的形参只能有一个
 *形参只能接收位置参数,而不能接收关键字参数
def fn3(*a) :
    print('a =',a)

**形参可以接收其他的关键字参数,它会将这些参数统一保存到一个字典中
  字典的key就是参数的名字,字典的value就是参数的值
**形参只能有一个,并且必须写在所有参数的最后

VI Notes python function

Function parameters determine the type of its scope.
After holding down the outside of the containers passed as a parameter to the function, if the function modifies the value inside the container, the outer container functions will be affected, but the outside of the normal function of the variables as arguments passed into the function, and function modification this is outside the parameters of the variables are not affected.

Published 59 original articles · won praise 213 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_42767604/article/details/105150344