zoukankan      html  css  js  c++  java
  • Python学习——装饰器/decorator/语法糖

    装饰器

    定义:本质是函数,为其他函数添加附加的功能。

    原则:1、不能修改原函数的源代码

       2、不能修改被原函数的调用方式

    重点理解:

       1、函数即“变量”

       2、高阶函数:返回值中包含函数名

       3、嵌套函数

    高阶函数 + 嵌套函数 = 装饰器

    热身: 先感受一下Python的解释器,结果可能和你预想的不同

    __author__ = 'jcx'
    
    def foo():
        print("in the foo")
        bar()  #解释器依次解释,先声明print后bar
     
    def bar():
         print("in the bar") #找到bar,定义
     
    foo() #只要在调用前声明就可以
    1 Output:
    2 in the foo
    3 in the bar

    应用:

    1、通用版 打印程序执行时间

     1 __author__ = 'jcx'
     2 
     3 import time
     4 
     5 def timer(func): #timer(test1)  func = test1
     6     def deco(*args, **kwargs): #这样写,就可以使被装饰的函数可以带参数
     7         start_time = time.time()
     8         func(*args, **kwargs)
     9         stop_time = time.time()
    10         print("Func RunTime = %s" % (stop_time - start_time))
    11     return deco
    12 
    13 @timer       #等于作了这部赋值 test1 = timer(test1)
    14 def test1():
    15     time.sleep(0.1)
    16     print("in the test1")
    17 
    18 @timer
    19 def test2(name,age):   #test2 = timer(test2)   test2() = deco()
    20     print("test2:", name,age)
    21 
    22 test1()  #实际是在执行deco()
    23 test2("jcx",24) #带参数
    24

    输出结果:

    1 in the test1
    2 Func RunTime = 0.10228705406188965
    3 test2: jcx 24
    4 Func RunTime = 2.09808349609375e-05

     2、多种方式登录网页/语法糖/嵌套

     1 __author__ = 'jcx'
     2 
     3 
     4 import time
     5 user,passwd = 'jcx', 'jcx123'
     6 
     7 def auth(auth_type):
     8     print("auth type: ", auth_type)
     9     def outer_wrapper(func):
    10         def wrapper(*args, **kwargs):
    11             # print("wrapper func: ", *args,**kwargs)
    12             if auth_type == "local":
    13                 username = input("Username:".strip())
    14                 password = input("Password:".strip())
    15                 if user == username and passwd == password:
    16                     print("33[32;1mUser has passed authentication33[0m")
    17                     res = func(*args, **kwargs)  #from home
    18                     print('-------->after authentication ')
    19                     return res
    20                 else:
    21                     exit("wrong username or password.")
    22             elif auth_type == "ldap":
    23                 print('waiting...')
    24         return wrapper
    25     return outer_wrapper
    26 
    27 @auth
    28 def index():
    29     print("Welcome to index page")
    30 
    31 @auth(auth_type = "local")   # home = auth() wrapper()  通过本地方式登录
    32 def home():
    33     print("Welcome to home page")
    34     return  "from home page"
    35 
    36 @auth(auth_type = "ldap")   #通过ldap方式登录
    37 def bbs():
    38     print("Welcome to bbs page")
    39 
    40 index("local")
    41 home()

     输出结果:

    1 auth type:  <function index at 0x10ae390e0>
    2 auth type:  local
    3 auth type:  ldap
    4 Username:jcx
    5 Password:jcx123
    6 User has passed authentication
    7 Welcome to home page
    8 -------->after authentication 
    9 waiting...
    ---------------- 生活 > 学习 >> 工作 ----------------
  • 相关阅读:
    四叔的c++入门课(6) 条件语句
    四叔的c++入门课(5)变量类型
    四叔的c++入门课程(4)赋值语句与表达式
    四叔的c++入门课(3)变量
    如何选购笔记本电脑?
    四叔的c++入门课(2)Hello World
    四叔的c++入门课(1)什么是编程
    CSS3 transition-property属性
    display:inline、block、inline-block的区别?
    wps怎样删除空白页 WPS文档的空白页如何删除
  • 原文地址:https://www.cnblogs.com/jcxioo/p/11580912.html
Copyright © 2011-2022 走看看