python-装饰器

1.闭包: 函数嵌套定义时,内层函数可以直接引用外层函数中的变量(想要修改。需要关键字nonlocal),而不是全局变量,这种现象称为闭包

value=10
def ff(value):
    value = value*10
    def gg():
        print("this is gg value is ",value)#引用的是ff函数内的value变量,而不是全局,这种情况就是闭包
    return gg  #返回函数
if __name__ == "__main__":
    ss=ff(10)
    ss()

运行结果:

     

 

2. 函数变量

   函数是一种的特殊的变量(实际存储的是函数的dizh地址),因此函数也是可以赋值的

def function():
    print("this is function")

if __name__ == "__main__":
    tt=function #将函数赋值给tt
    print(type(tt))
    print(type(function))
    tt() #调用函数

其结果:

    

    test和function代表hans函数地址,test()和function()代表函数调用

 

3. 高阶函数

   上面介绍过,函数名称可以理解为普通的变量,那么就可以作为函数参数或者函数返回值(和普通变量使用相同)。

    3.1作为函数参数

def test():
    print("test is running!")

def deco(func):  
    print("函数开始运行")
    func() 
    print("函数运行结束")


if __name__ == "__main__":
    deco(test)

运行结果

  

   上述例子中,将test(函数变量)作为参数传递给deco函数,其实也就是func = test, 在deco函数中直接调用test函数。

    3.2 作为函数返回值

def test1():
  print("this is function test1")  


def test():
    return test1

if __name__ == "__main__":
    
    ss=test()
    ss()

   其运行结果:

         

 

4.函数嵌套

   

def test():
    def test1():
        print("this is function test1")
   
    print("this is test")

if __name__ == "__main__":
    
    test()

  需要注意

  1.  test1函数只能调用和它同级别以及上级的变量或函数

   2. 调用test函数时不会主动调用test1函数

     

 

5.装饰器

 装饰器,简单来说就是在不修改原有函数代码的情况下,增加新的功能。为了更好的理解裂解装饰器是怎么实现的,本文将会以实际案例来讲解。

假设有N个模块,每个模块中都含有一个函数,实现简单的问候功能,具体实现如下:

def print_hello():
    print("hello,welcom to use our programe  xxx")#xxx为具体模块的名称

  现在公司要求,所有的问候功能,需要打印出当前的时间。因此,每个模块都对自己的代码进行修改

  模块1代码修改如下:

def print_hello():
    print(time.time())
    print("hello,welcom to use our programe  model1")

模块2修改如下:

def print_hello():   
    print("hello,welcom to use our programe  model2")
    print(time.localtime())

因为没有统一的标准,每个模块更改的方式都不同,因此公司统一格式,各个模块再次修改

def print_hello():
    print(time.time())
    print("hello,welcom to use our programe  xxx")

为了改进产品,公司现在计划在问候功能中再增加部分功能,打印当前天气情况,但是考虑到之前遇到的问题,公司不希望各个模块自行修改,因此,有人提出,可以通过传递函数的方式,在不修改原有代码的情况下,增加新的功能,代码如下:

def print_hello():
    print(time.time())
    print("hello,welcom to use our programe  xxx")

def print_hello_new(func):
    print("当前的天气情况是 xxx")
    func()


print_hello_new(print_hello)

 但是,此种做法改变了函数的名称,并且zen增加了参数的传递,考虑到xuya需要修改的地方太多,因此放弃。

 

   这时候,又有人提出,既然不想修改函数的名称和调用方式,可以通过返回函数的方法,将返回值函数的返回值重新赋值给原来的函数名称,具体实现如下:

def print_hello():
    print(time.time())
    print("hello,welcom to use our programe  xxx")

def print_hello_new(func):#传递函数
    def new():#函数嵌套
        print("当前的天气情况是 xxx")
        func()
    return new #返回函数


ss=print_hello_new(print_hello)#函数赋值,此时只执行了print_hello_new函数,new函数不会自动执行
ss()

上述的功能实现了主要的功能,但是写法比较比较复杂,python支持装饰器操作,可以简化上面的操作:

import time
def print_hello_new(func):
    def new():
        print("当前的天气情况是 xxx")
        func()
    return new

@print_hello_new #装饰器语法 相当于ss=print_hello_new(print_hello)
def print_hello():
    print(time.time())
    print("hello,welcom to use our programe  xxx")


print_hello()

  执行结果

至此,装饰器的部分已经介绍完毕,总结起来,装饰器 = 函数赋值 + 函数嵌套 + 高阶函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值