zoukankan      html  css  js  c++  java
  • python-装饰器

    1.装饰器:
    本质是函数,
    定义:装饰其他函数,就是为其他函数添加附加功能。
    原则:装饰器对被装饰的函数完全透明
    1.不能修改被装饰的函数的源代码
    2. 不能修改被装饰的函数的调用方式
     
     #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # Auther: Summer


    1
    # 装饰器本身带参数 2 3 user, passwd = 'summer','abc123' 4 5 def auth(auth_type): 6 print('auth_type', auth_type) 7 def outer_wrapper(func): 8 def wrapper(*agrs,**kwargs): 9 print("wrapper func",*agrs,**kwargs) 10 if auth_type == 'local': 11 username = input('please input your username:').strip() 12 password = input('please input your password:').strip() 13 if user == username and passwd == password: 14 print("User has passed authentication") 15 res = func(*agrs,**kwargs) 16 print("----after authentication") 17 return res 18 else: 19 print("Invalid username and password!") 20 exit() 21 elif auth_type == 'ldap': 22 print("buhui") 23 return wrapper 24 return outer_wrapper 25 26 @auth('local') 27 def index(): 28 print('welcom to index page') 29 @auth(auth_type = 'local') # 相当于home = auth() 执行auth函数里面的outer_wrapper 30 def home(): 31 print('welcome to home page') 32 @auth(auth_type = 'ldap') 33 def bbs(): 34 print('welcome to bbs page') 35 36 index() 37 home() 38 bbs()
    实现装饰器只是储备:
    1.函数即变量
    2. 高阶函数
     1 #!/usr/bin/env python
     2 # -*- coding: utf-8 -*-
     3 # Auther: Summer
     4 
     5 # 返回值中包含函数名
     6 import time
     7 
     8 
     9 def bar():
    10     time.sleep(3)
    11     print('in the bar')
    12 
    13 def test2(func):   # 新功能
    14     print(func)
    15     return func
    16 
    17 # test2(bar)
    18 # print(test2(bar))
    19 t = test2(bar)
    20 print(t)   # 打印bar的内存地址
    21 t()        # 运行bar
    22 bar = test2(bar)   #
    23 bar()      # bar中加上了test2的功能,没有修改bar的调用方式
    3.嵌套函数
    高阶函数+嵌套函数 =》 装饰器
     
    2.匿名函数
    calc = lambda x:x*3
    高阶函数:
    1.把一个函数名当做实参传给另外一个函数(不修改被装饰函数源代码的情况下为其添加功能)
    2.返回值中包含函数名(不修改函数的调用方式)
     
    3. 嵌套函数:
    在一个函数的函数体内用def去声明一个函数
    通用装饰器
     1 # 函数的嵌套
     2 # 在一个函数的函数体内用def去声明一个函数
     3 
     4 def foo():
     5     print('in the foo')
     6 
     7     def bar():    #具有局部变量的特性,只能在内部调用
     8         print("in the bar")
     9     bar()
    10 foo()
    11 
    12 
    13 x = 0
    14 def grandpa():
    15     x =1
    16     def dad():
    17         x =2
    18         def son():
    19             x = 3
    20             print(x)   # 函数的作用域,从里往外找
    21         son()
    22     dad()
    23 grandpa()
     1 # 通用装饰器,可传参
     2 '''
     3 1.不修改被装饰的函数的源代码
     4 2. 不修改被装饰的函数的调用方式
     5 '''
     6 
     7 import time
     8 
     9 def timer(func):
    10     def deco(*args,**kwargs):   # 定义一个高阶函数
    11         start_time = time.time()
    12         func(*args,**kwargs)   # 运行bar
    13         stop_time = time.time()
    14         print("the func run time is %s" %(stop_time-start_time))
    15     return deco
    16 
    17 @timer
    18 def test1():
    19     time.sleep(1)
    20     print('in the test1')
    21 
    22 @timer
    23 def test2(name,age):
    24     time.sleep(1)
    25     print('in the test2:',name,age)
    26 
    27 test1()
    28 test2('summer',24)
  • 相关阅读:
    mysql语句查询:查询距离某个日期10天的数据
    Http中cookie的使用以及用CookieManager管理cookie
    HttpURLConnection绕过HTTPS的SSL验证
    echarts的简单应用之(二)饼图
    echarts的简单应用之(一)柱形图
    java中字节流与字符流的区别
    对SpringMVC框架的理解(转)
    opencv之yaml和xml文件读写
    C++之读取和写入文件
    opencv之投影点云到图像中
  • 原文地址:https://www.cnblogs.com/summerxye/p/8203580.html
Copyright © 2011-2022 走看看