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() #只要在调用前声明就可以
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/语法糖的更多相关文章
- python中装饰器(语法糖)概念
“”“” 什么是装饰器? """ 还是通过一个例子来慢慢说明 先看下面的例子 def func_1(x): return x*2 def fun_2(x): return ...
- Python学习---装饰器/迭代器/生成器的学习【all】
Python学习---装饰器的学习1210 Python学习---生成器的学习1210 Python学习---迭代器学习1210
- day13 装饰器与语法糖
day13 装饰器与语法糖 一.装饰器 1.什么是装饰器 装饰器就是装饰别人的工具,具体是指为被装饰者添加新功能 装饰器->函数 被装饰者->函数 2.为何要用装饰器 装饰器的核心思想:( ...
- 详解python的装饰器decorator
装饰器本质上是一个python函数,它可以让其它函数在不需要任何代码改动的情况下增加额外的功能. 装饰器的返回值也是一个函数对象.它经常用于有切面需求的场景,比如:插入日志,性能测试,事务处理,缓存, ...
- python函数编程-装饰器decorator
函数是个对象,并且可以赋值给一个变量,通过变量也能调用该函数: >>> def now(): ... print('2017-12-28') ... >>> l = ...
- Python学习---装饰器的学习1210
装饰器的基础 学习前提: 作用域 + 函数的理解 + 闭包 [学习,理解] 代码编写原则: 对修改开放对扩展开放 装饰器本质上是一个函数,该函数用来处理其他函数,它可以让其他函数在不需要修改代码的前 ...
- Python基础之函数:2、globlal与nonlocal和闭包函数、装饰器、语法糖
目录 一.global与nonlocal 1.global 2.nonlocal 二.函数名的多种用法 三.闭包函数 1.什么是闭包函数 2.闭包函数需满足的条件 3.闭包函数的作用 4.闭包函数的实 ...
- python学习---装饰器
什么是装饰器 器即函数 装饰即修饰,意指为其他函数添加新功能 装饰器定义:本质就是函数,功能是为其他函数添加新功能 装饰器需要遵循的原则 1.不修改被装饰函数的源代码(开放封闭原则) 2.为被装饰函数 ...
- python_装饰器_语法糖
什么是高阶函数? -- 把函数名当做参数传给另外一个函数,在另外一个函数中通过参数调用执行 #!/usr/bin/python3 __author__ = 'beimenchuixue' __blog ...
随机推荐
- 一种新的python局部调试手法
我们都知道,python里面可以用pdb来调试代码.但是pdb往往不大好用.有时候调试代码往往在多重条件里面,直接用pdb需要下条件断点,设定复杂的条件. 一个简单的办法就是这么干. __import ...
- Angularjs中controller的三种写法
在Angular中,Directive.Service.Filter.Controller都是以工厂方法的方式给出,而工厂方法的参数名对应着该工厂方法依赖的Service.angularjs中cont ...
- 十九 Listener
Listener 监听器 一 监听器内部原理:其实就是接口回调 需求:A在执行循环,当循环到5的时候,通知B 事先先把某一个对象传递给A ,当A执行到5的时候,通过这个对象来调用B中的方法 但是不是直 ...
- thinkphp的增删改查命令 - (mysql-thinkphp) (4)
方法1,在namespace下面加2行 use think\Controller; use think\Db; 1.查询所有结果 $res = Db::query("select * fro ...
- springboot的maven多模块项目架构微服务搭建——构建多模块项目(依赖方式)
总想对微服务架构做一个小小的总结,不知如何下手,最近觉得还是从搭建微服务的过程来入手,对于springboot的maven项目从构建多模块架构进而衍化为常用的微服务架构来做个记录吧. 首先,创建多个s ...
- Linux-Journal
Linux-Journal 1. 日志简介 2. 日志的优先级和分类 2.1 优先级 2.2 设施分类 3. 命令帮助 4. 日志查看示例 5. 日志大小限制 6. 手动清理日志文件 1. 日志简介 ...
- 使用vue框架开发前端项目的步骤
前端项目的开发 1. 本地安装nodejs https://nodejs.org/en/download/ 2. 测试安装 > node -v 3. 本地安装git > git --ver ...
- 0103-springmvc的基本流程
背景 现在的it研发,已经从管理系统时代迈入了互联网系统时代. 页面开发已经从基于JSP+struts转变为为前后端分离的方式(springMVC + JS): 思想 MVC mvc框架不仅适用于ja ...
- Service总结
Service Service的应用场景,以及和Thread区别 开启Service的两种方式以及区别 Service基础 Service是什么? Service(服务)是一个可以在后台长时间运行而没 ...
- ①spring简介以及环境搭建(一)
注*(IOC:控制反转.AOP:面向切面编程) spring官网:http://spring.io/ spring简介: spring是一个开源框架 spring为简化企业级应用开发而生,使用Spri ...