python里面有两个函数语法结构:装饰器和偏函数(functools.partial())
- 装饰器@
这里记录下自己理解的装饰器:装饰器就是装饰现有函数,现有函数不需要有任何代码修改的情况下,增加函数的功能。
装饰器函数:
def logging(level):
def warpper(func):
def inner_warpper(*args,**kwargs):
print("{level}:enter function {func}".format(level=level,func=func.__name__) )
return func(*args,**kwargs)
return inner_warper
用装饰器来穿参数:
@logging(level="INFO")
def say( str):
print("say {}".format(str))
@logging(level="DEBUG")
def do(str):
print("do {}".format(str))
say("hello")
do("my work")
装饰器促发后的执行过程:当某个函数加上@装饰器以后,这个装饰器马上被执行,上面例子中,就是logging()函数被执行。而logging函数的返回值,才开始执行被装饰得函数say()。
- 偏函数
偏函数的语法为functools.partial( self,*args,**kwargs)
偏函数也可以和装饰器一样,不改变原有函数的代码,来修饰函数的功能。达到简介代码的目的。
两者的区别如下:装饰器对函数进行了二次包装,改变了被装饰函数的函数入口地址,而偏函数是直接产生来一个新函数,完全不影响原函数。
偏函数举例
def sum(*args):
s = 0
for x in args:
s+=x
return s
//这里用偏函数创造一个新的函数
sum_ex = functools.partial(sum , 100 , 200)
print(sum)
print(sum_ex)
这两个函数地址变了
print(sum_ex(1,2,3,4,5))
输出:315
这个函数证明:
1、原函数和偏函数的地址不一样了。
2、偏函数提前固定了2个参数,包装啦一层函数,简化了函数的调用,产生来新的函数地址。
- 总结
偏函数和装饰器有相似的地方,都是为了不改变原始函数,进而扩展代码,优化代码。
偏函数是固定函数的参数,装饰器是增加函数的功能,两者可以结合使用。
装饰器结合偏函数
import functools
import time
class DelayMan(object):
def __init__(self,durations ,func):
super().__init__()
self.durations=durations
self.func = func
def __call__(self,*args,**kwargs):
print("please wait for %s seconds ..."% self.durations)
time.sleep(slef,durations)
return self.func(*args,**kwargs)
def no_delay_man(self,*args,**kwargs):
print("call immediately ....")
return self.func(*args,**kwargs)
//定义偏函数
def delay(durations):
return functools.partial(DelayMan, durations)
//对add函数添加装饰器
@delay(5)
def add(*args):
return sum(args)
执行函数
print(add(1,2,3,4,5,6))
print(add.no_delay_man(2))
执行函数时候发生了啥:
第一步:执行装饰器@delay,这个装饰器函数是一个偏函数,是一个新的类了,不再是原始的类DelayMan。
第二步:delay装饰器传入的参数是durations=5,然后进入DelayMan类,传入被装饰的函数add。
第三步:执行add()函数。