python basic grammar _9-1 closure decorator supplement

1, the concept of closures
closure: internal functions referenced variables enclosing scope, external function returns the internal function name
 
2, function and properties of the substance
An object function: there is a storage space in memory
After completion of the internal variable function performed Recovery: reference count is not zero
Function attributes: Special Properties
Function return value
example
passline = 60
def func(val):
print ( '% x'% id (val)) # View variable name address
if val >= passline:
print('pass')
else:
print('failed')
def in_func (): # (val,) is added after the tuples can not change
print (val) # reference variable after variable to explain functions within the property, including a direct function when you use the Find
in_func()
return in_func
# func(88)
a = func (88) # variable names that refer in_func
print (a .__ name__) # View variable names
a () # actually called in_func (), equivalent to a in_func and point to the same function, a function name only and in_func
print (a) # view in_func () function func address attribute
print (a .__ closure__) # view in_func () in the property exists val variable, closure properties
3, action and advantages of the closures
  • Achieve the function package
  • Improve code reuse
 
4, the common part of the code is extracted, instead of the drawing portion in the form of closure, the function to be used as a parameter
example:
Two code duplication method:
def my_sum(*arg):
if len (arg) == 0: # dividend can not be 0
return 0
for val in arg: # int data type can only be
if not isinstance(val,int):
return 0
return sum(arg)
def my_average(*arg):
if len(arg) == 0:
return 0
for val in arg:
if not isinstance(val,int):
return 0
return sum(arg)//len(arg)
 
print(my_sum(1,2,3,'2'))
print(my_average())
重构后:
def my_sum(*arg):
return sum(arg)
def my_average(*arg):
return sum(arg)//len(arg)
def dec(func):
def in_dec(*arg):
if len(arg) == 0: # 被除数不能为0
return 0
for val in arg: # 数据只能是int型
if not isinstance(val,int):
return 0
return func(*arg)
return in_dec
my_sum = dec(my_sum) # 1、先调用dec,2、再将my_sum函数名指向函数in_dec
my_average = dec(my_average)
 
# 当使用my_sum函数时,3、调用in_dec,4、再调用my_sum
print(my_sum(1,2,3,'2'))
print(my_average())
 
5、装饰器:
 
  • 装饰器用来装饰函数
  • 返回一个函数对象
  • 被装饰函数标识符指向返回的函数对象
  • 语法:@deco
装饰器实质是对闭包的使用
def dec(func):
print('1、call dec')
def in_dec(*arg):
print('3、call in_dec')
if len(arg) == 0:
return 0
for val in arg:
if not isinstance(val,int):
return 0
return func(*arg)
print('2、return in_dec')
return in_dec
print('装饰器代码执行顺序:')
@dec # 等于my_sum = dec(my_sum)这句话
def my_sum(*arg): # 装饰过后my_sum名指向in_dec函数对象,此时in_dec会调用原来的my_sum,要使用my_sum(1,2)才能调用
print('4、call my_sum')
return sum(arg)
# print(sum(arg))
def average(*arg):
return sum(arg)//len(arg)
 
print('调用被修饰的函数后才会执行闭包中的函数')
print(my_sum(1,2,3))
输出结果:
装饰器代码执行顺序:
1、call dec
2、return in_dec
调用被修饰的函数后才会执行闭包中的函数
3、call in_dec
4、call my_sum

 

Guess you like

Origin www.cnblogs.com/TomBombadil/p/10979397.html