python_005_ anonymous function, the closure function, recursive function

I. anonymous function

# Anonymous function: to help developers complete a simple business logic 
. "" "
Syntax
lambda parameter 1, parameter 2, 3 ... parameters: function body
Note the code inside the function body can only exist in an expression, if the expression is evaluated after there results, at which time the results of an anonymous function as a return value back to the outside world
. "" "

#fun1 = lambda x, y, **kwargs: max(x, y)

#num = fun1(1,2)
#print(num)

 

II. Closure function

 

# Closure function 
"" "
two functions A, B, if the function A is defined inside the function B, A and B as a function of the return value to the outside, when the closure function B is called the A
closure function effect : closure function may delay local variable is recovered system time, to ensure that dynamic variables can be used in accordance with the needs of the user
"" "
Import Random
# function a
DEF getRandomSum (min, max, NUM):
List1 = [the random.randint (min , max) for I in Range (NUM)]
Print (List1)
# function B
DEF GetSum ():
Print (List1)
return SUM (List1)

return GetSum

# calling function a, while the function returns after a complete closing performing a function package B
fun1 getRandomSum = (10,50,5)
Print (fun1 ())


 

III. Recursive function

 

# Recursive function 
"" "
defined recursive functions:
calling a function A of the same name and function inside the function A, the function will be submitted at this time based on layer by layer to achieve the task of internal code
Note:
1, recursive function first task is completed submit test layer by layer, thus decreasing the task must be able to terminate
2, followed by the task will be executed, perform up layer by layer from the last start, until the end of the outermost call
. "" "
# 5 factorial
DEF Jiecheng (the n-):
IF the n-= . 1 =:
return. 1
the else:
return Jiecheng n-* (. 1-n-)

Print (Jiecheng (. 5))

 

四 回掉函数(先进再出)
"""
A,B两个函数,B函数作为A函数调用时的一个参数被传入到A函数内部,在A内部调用B函数,此时B函数称为A函数的回掉函数
回掉函数一般情况下会作为主函数A的一个条件传入,目的是对A函数产生的结果做出处理,从而实现对A函数功能的扩充
"""
# 定义A函数(完成对所有可迭代数据的遍历)
def mapList(data, fun):#fun是一个函数
for item in data:
fun(item)#调用函数

#回掉函数B(输出遍历得到的元素)
def outPut(num):
print(num)

#定义回掉函数完成求列表数据的和
sum_num = 0
def sumNum(num):
global sum_num
sum_num += num

mapList(range(1, 10), outPut)#函数B相当于fun中的参数,在函数A中被调用
mapList(range(1,10), sumNum)#内部函数被外界调用,回掉函数完成求列表数据的和
print(sum_num)
五,装饰器
"""
装饰器的作用:在不改变原函数任何代码情况下对原函数功能进行升级
"""
# 定义函数完成对参与运算的数据类型的比对
import functools#functools 作用于函数的函数 functools 模块提供用于调整或扩展函数和其他可调用对象的工具
def adjust(fun):
# 将被修饰函数的名字绑定给wrapper
@functools.wraps(fun)
def wrapper(*args, **kwargs):#闭包(可变参数,命名关键字参数)
kind = type(args[0])#类型判断
or_common = True
for item in args:
if kind != type(item):
or_common = False
break
if or_common:
return fun(*args, **kwargs)
else:
print('数据类型不匹配,无法完成运算')
return None
return wrapper
"""
@adjust在修饰qiuhe函数时等价于
qiuhe = adjust(qiuhe)
#外部求和qiuhe==fun
#装饰器调用
#fun==wrapper==qiuhe
"""
@adjust
def qiuhe(num1, num2, num3):
return num1+num2+num3

print(qiuhe.__name__)#求和的名字
# qiuhe = adjust(qiuhe)

@adjust
def qiucha(num1, num2):
return num1-num2
@adjust
def qiuji(num1, num2):
return num1*num2

print(qiuhe(10,"30", 30))
import functools
#带参数装饰器
def logs(log):
def adjust(fun):
print(log)
@functools.wraps(fun)
def wrapper(*args, **kwargs):
kind = type(args[0])
or_common = True
for item in args:
if kind != type(item):
or_common = False
break
if or_common:
return fun(*args, **kwargs)
else:
print('类型不匹配')
return None
return wrapper
return adjust

@logs('当前运行的是第22行代码')
def qiuhe(*args):
num_sum = 0
for item in args:
num_sum += item
return num_sum
# 带参数的装饰器修饰函数时等价于
# qiuhe = logs('当前运行的是第22行代码')(qiuhe)
print(qiuhe(10,20,30,40))
 

Guess you like

Origin www.cnblogs.com/t-a-n-g-124798/p/11236616.html