zoukankan      html  css  js  c++  java
  • Python3--装饰器详解

    装饰器

    定义:本质是函数.(装饰其他函数)就是为其他函数添加附加功能

    原则:1.不能修改被装饰的函数的源代码

       2.不能修改被装饰的函数的调用方式

    理解装饰器前提条件:

    1.函数即"变量"

    2.高阶函数

      a.把一个函数名当做实参传给另一个函数(在不修改被装饰函数源代码的情况下为其添加功能)

      b.返回值中包含函数名(不修改函数的调用方式)

    3.嵌套函数

      1 import time
      2 
      3 ################################### 高阶函数 ################################################################
      4 
      5 '''
      6 高阶函数
      7 '''
      8 #普通函数
      9 def foo():
     10     #阻塞两秒
     11     time.sleep(2)
     12     print("this is foo")
     13 #高阶函数
     14 def bar(func):
     15     start_time = time.time()
     16     func()
     17     end_time = time.time()
     18     print("this is func time %s"%(end_time - start_time))
     19     return func
     20 #初始调用foo函数的方式
     21 # foo()
     22 #经过高阶函数调用foo函数的方式
     23 foo = bar(foo)
     24 foo()
     25 
     26 ################################### 嵌套函数 ################################################################
     27 
     28 '''
     29 嵌套函数
     30 '''
     31 def bar():
     32     start_time = time.time()
     33     def foo():
     34         # 阻塞两秒
     35         time.sleep(2)
     36         print("this is foo")
     37     #调用普通函数
     38     foo()
     39     end_time = time.time()
     40     print("this is func time %s" % (end_time - start_time))
     41 
     42 #调用嵌套函数
     43 bar()
     44 
     45 ################################### 简易版装饰器 ################################################################
     46 
     47 '''
     48 高阶函数 + 嵌套函数 => 装饰器
     49 '''
     50 #简易版装饰器---函数不可传入参数
     51 def bar(func):
     52     def inner():
     53         start_time = time.time()
     54         func()
     55         end_time = time.time()
     56         print("this is func time %s" % (end_time - start_time))
     57     return inner
     58 
     59 #普通函数
     60 @bar
     61 def foo():
     62     # 阻塞两秒
     63     time.sleep(2)
     64     print("this is foo")
     65 
     66 #普通调用方式
     67 foo()
     68 
     69 ################################### 升级版装饰器 ################################################################
     70 
     71 #升级版装饰器---函数可以传入参数
     72 def bar(func):
     73     def inner(*args,**kwargs):
     74         start_time = time.time()
     75         func(*args,**kwargs)
     76         end_time = time.time()
     77         print("this is func time %s" % (end_time - start_time))
     78     return inner
     79 
     80 #普通函数
     81 @bar
     82 def foo(name,age):
     83     # 阻塞两秒
     84     time.sleep(2)
     85     print(" %s this is foo %s"%(name,age))
     86 
     87 @bar
     88 def deco():
     89     # 阻塞两秒
     90     time.sleep(2)
     91     print(" this is deco ")
     92 
     93 #普通调用方式
     94 deco()
     95 foo('zhangsan',22)
     96 
     97 ################################### 高级版装饰器 ################################################################
     98 
     99 #高级版装饰器---装饰器与函数都可以传入参数,函数装饰内容可以根据装饰器参数做判断
    100 #应用场景---在做用户验证时可以使用不同的验证手段,如本地验证,数据库验证,网络验证
    101 def decorator(bath_type):
    102     def bar(func):
    103         def inner(*args,**kwargs):
    104             if bath_type == 'fooname':
    105                 start_time = time.time()
    106                 func(*args,**kwargs)
    107                 end_time = time.time()
    108                 print("this is fooname time %s" % (end_time - start_time))
    109             elif bath_type == 'deconame':
    110                 start_time = time.time()
    111                 func(*args, **kwargs)
    112                 end_time = time.time()
    113                 print("this is deconame time %s" % (end_time - start_time))
    114         return inner
    115     return bar
    116 
    117 #普通函数
    118 @decorator(bath_type='fooname')
    119 def foo(name,age):
    120     # 阻塞两秒
    121     time.sleep(2)
    122     print(" %s this is foo %s"%(name,age))
    123 
    124 @decorator(bath_type='deconame')
    125 def deco():
    126     # 阻塞两秒
    127     time.sleep(2)
    128     print(" this is deco ")
    129 
    130 #普通调用方式
    131 deco()
    132 foo('zhangsan',22)
  • 相关阅读:
    css 旋转
    html 旋转
    链表和数组的区别
    hashmap
    【java开发系列】—— 自定义注解
    java不确定参数个数方法例子
    mysql 删除
    linux下常用命令
    php的几种算法(转载)
    数据容器
  • 原文地址:https://www.cnblogs.com/weijiazheng/p/10553012.html
Copyright © 2011-2022 走看看