装饰器

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

原则: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() #只要在调用前声明就可以
 Output:
in the foo
in the bar

应用:

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

 __author__ = 'jcx'

 import time

 def timer(func): #timer(test1)  func = test1
def deco(*args, **kwargs): #这样写,就可以使被装饰的函数可以带参数
start_time = time.time()
func(*args, **kwargs)
stop_time = time.time()
print("Func RunTime = %s" % (stop_time - start_time))
return deco @timer #等于作了这部赋值 test1 = timer(test1)
def test1():
time.sleep(0.1)
print("in the test1") @timer
def test2(name,age): #test2 = timer(test2) test2() = deco()
print("test2:", name,age) test1() #实际是在执行deco()
test2("jcx",24) #带参数
24

输出结果:

 in the test1
Func RunTime = 0.10228705406188965
test2: jcx 24
Func RunTime = 2.09808349609375e-05

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

 __author__ = 'jcx'

 import time
user,passwd = 'jcx', 'jcx123' def auth(auth_type):
print("auth type: ", auth_type)
def outer_wrapper(func):
def wrapper(*args, **kwargs):
# print("wrapper func: ", *args,**kwargs)
if auth_type == "local":
username = input("Username:".strip())
password = input("Password:".strip())
if user == username and passwd == password:
print("\033[32;1mUser has passed authentication\033[0m")
res = func(*args, **kwargs) #from home
print('-------->after authentication ')
return res
else:
exit("wrong username or password.")
elif auth_type == "ldap":
print('waiting...')
return wrapper
return outer_wrapper @auth
def index():
print("Welcome to index page") @auth(auth_type = "local") # home = auth() wrapper() 通过本地方式登录
def home():
print("Welcome to home page")
return "from home page" @auth(auth_type = "ldap") #通过ldap方式登录
def bbs():
print("Welcome to bbs page") index("local")
home()

输出结果:

 auth type:  <function index at 0x10ae390e0>
auth type: local
auth type: ldap
Username:jcx
Password:jcx123
User has passed authentication
Welcome to home page
-------->after authentication
waiting...

Python学习——装饰器/decorator/语法糖的更多相关文章

  1. python中装饰器(语法糖)概念

    “”“” 什么是装饰器? """ 还是通过一个例子来慢慢说明 先看下面的例子 def func_1(x): return x*2 def fun_2(x): return ...

  2. Python学习---装饰器/迭代器/生成器的学习【all】

    Python学习---装饰器的学习1210 Python学习---生成器的学习1210 Python学习---迭代器学习1210

  3. day13 装饰器与语法糖

    day13 装饰器与语法糖 一.装饰器 1.什么是装饰器 装饰器就是装饰别人的工具,具体是指为被装饰者添加新功能 装饰器->函数 被装饰者->函数 2.为何要用装饰器 装饰器的核心思想:( ...

  4. 详解python的装饰器decorator

    装饰器本质上是一个python函数,它可以让其它函数在不需要任何代码改动的情况下增加额外的功能. 装饰器的返回值也是一个函数对象.它经常用于有切面需求的场景,比如:插入日志,性能测试,事务处理,缓存, ...

  5. python函数编程-装饰器decorator

    函数是个对象,并且可以赋值给一个变量,通过变量也能调用该函数: >>> def now(): ... print('2017-12-28') ... >>> l = ...

  6. Python学习---装饰器的学习1210

    装饰器的基础 学习前提: 作用域 + 函数的理解 + 闭包  [学习,理解] 代码编写原则: 对修改开放对扩展开放 装饰器本质上是一个函数,该函数用来处理其他函数,它可以让其他函数在不需要修改代码的前 ...

  7. Python基础之函数:2、globlal与nonlocal和闭包函数、装饰器、语法糖

    目录 一.global与nonlocal 1.global 2.nonlocal 二.函数名的多种用法 三.闭包函数 1.什么是闭包函数 2.闭包函数需满足的条件 3.闭包函数的作用 4.闭包函数的实 ...

  8. python学习---装饰器

    什么是装饰器 器即函数 装饰即修饰,意指为其他函数添加新功能 装饰器定义:本质就是函数,功能是为其他函数添加新功能 装饰器需要遵循的原则 1.不修改被装饰函数的源代码(开放封闭原则) 2.为被装饰函数 ...

  9. python_装饰器_语法糖

    什么是高阶函数? -- 把函数名当做参数传给另外一个函数,在另外一个函数中通过参数调用执行 #!/usr/bin/python3 __author__ = 'beimenchuixue' __blog ...

随机推荐

  1. java Spring整合Freemarker的详细步骤

    java Spring整合Freemarker的详细步骤 作者: 字体:[增加 减小] 类型:转载 时间:2013-11-14我要评论 本文对Spring整合Freemarker步骤做了详细的说明,按 ...

  2. 命令行添加删除tomcat服务

    在进行项目布署的时候,有时候我们需要重新添加或者卸载tomcat服务,这里,我们可以在命令提示符下进行操作,步骤如下:1.开始—运行,敲入cmd,回车打开命令提示符.2.进行tomcat所在盘符,如d ...

  3. Tomcat+JSP经典配置实例

    经常看到jsp的初学者问tomcat下如何配置jsp.servlet和bean的问题,于是总结了一下如何tomcat下配置jsp.servlet和ben,希望对那些初学者有所帮助. 一.开发环境配置 ...

  4. sqlite帮助类

    帮助类 using System; using System.Collections.Generic; using System.Data; using System.Data.SQLite; usi ...

  5. Python学习笔记之基础篇(二)python入门

    一.pycharm 的下载与安装: 使用教程:https://www.cnblogs.com/jin-xin/articles/9811379.html 破解的方法:http://xianchang. ...

  6. Windows驱动开发-_DRIVER_OBJECT结构体

    每个驱动程序会有唯一的驱动对象与之对应,并且这个驱动对象是在驱动加载的时候,被内核中的对象管理程序所创建的,内核对一个驱动只加载一个实例,是由内核中的I/O管理器负责加载的,驱动程序需要在Driver ...

  7. python merge、join、concat用法与区别

     由于合并变化较大,以后函数可能会修改,只给出一些例子作为参考 总结: merge.join 1.当没有索引时:merge.join为按照一定条件合并 2.当有索引.并按照索引合并时,得到结果为两者混 ...

  8. 弱点扫描-openvas初始化

    OPENVAS: NESSUS项目分支:商业版的扫描器 管理目标系统的漏洞 免费开源 Kali 默认安装但是未配置个启动 安装 创建证书 同步弱点数据库 创建客户端证书 重建数据库 备份数据库 启动服 ...

  9. 站在巨人的肩膀上才能看得更加远[Amo]

    本来只是路过,写详细一点. 我看楼主浮躁得不得了.现在什么都不要做了,先去看几遍<不要做浮躁的嵌入式工程师>这篇文章,想清楚了,再动手吧. 我做了个实例,不用ST的库来点LED,解答你的问 ...

  10. java#java.util.Date与java.time.LocalDateTime的转换

    转换主要通过Instant来进行的. Date date = new Date(); LocalDateTime localDateTime = LocalDateTime.ofInstant(dat ...