Python学习第十一课——装饰器
#装饰器:本质就是函数,为其他函数附加功能
原则:
1、不修改被修饰函数的源代码
2、不修改被修饰函数的调用方式
装饰器=高阶函数+函数嵌套+闭包
#高阶函数
'''
高阶函数定义:
1、函数接受的参数是一个函数名
2、函数的返回值是一个函数名
3、满足上述条件任意一个,都可称之为高阶函数
''' # 函数接受的参数是一个函数名的情况下,要改变函数的调用方式
def foo():
time.sleep(2)
print("憨憨你好") def test(func):
print(func)
start_time=time.time()
func()
stop_time=time.time()
print('函数的运行时间是%s' %(stop_time-start_time)) test(foo) # 修改了函数的调用方式 # 函数的返回值是一个函数名,此时不需要改变函数调用方式
def foo():
print('from the foo') def test(func):
return func foo = test(foo)
foo() def foo():
time.sleep(3)
print('来自foo') # 完成装饰器添加功能
# 该写法多运行了一次 所以高阶函数无法满足装饰器运用
def timmer(func):
start_time = time.time()
func()
stop_time = time.time()
print('函数运行时间%s' % (stop_time - start_time))
return func foo = timmer(foo)
foo()
#函数镶嵌
def father(name):
print('from father %s' % name) def son():
print('他的爸爸是%s' % name) def grandson():
print('他的爷爷是%s' % name) grandson() son() father('憨憨')
#装饰器例子
# 打印1+2+3+..+100 并且打印出运行时间
import time def cal(l):
start_time = time.time()
res = 0
for i in l:
time.sleep(0.01)
res += i
stop_time = time.time()
print("函数的运行时间是%s" % (stop_time - start_time))
return res print(cal(range(100))) def timmer(func): # 计算函数运行时间功能 def wrapper(*args, **kwargs):
start_time = time.time()
res = func(*args, **kwargs)
stop_time = time.time()
print('函数运行的时间是%s' % (stop_time - start_time))
return res return wrapper @timmer # 在不动原函数的基础上 为该函数加上其他功能
def cal(f):
res = 0
for i in f:
time.sleep(0.1)
res += i
return res print(cal(range(100)))
#函数镶嵌 装饰器的实现
def timmer(func):
def wrapper():
start_time = time.time()
func() # 运行的就是test
stop_time = time.time()
print('运行时间是%s' % (stop_time - start_time)) return wrapper @timmer # 相当于 test=timmer(test)
def test():
time.sleep(3)
print('test已经运行') test()
# 例子1:为京东商城中的一些方法加上验证装饰器
# 将下列方法加上验证装饰器
current_dic = {'username': None, 'login': False} user_list = [
{'name': 'hanhan', 'pwd': ''},
{'name': 'amei', 'pwd': ''},
{'name': 'ahao', 'pwd': ''}
] def yanzheng(func):
def wrapper(*args, **kwargs):
if current_dic['username'] and current_dic['login']:
res = func(*args, **kwargs)
return res
username = input('输入用户名:').strip()
pwd = input('输入密码:').strip()
for user_dic in user_list:
if username == user_dic['name'] and pwd == user_dic['pwd']:
current_dic['username'] = username
current_dic['login'] = True
res = func(*args, **kwargs)
return res
else:
print('输入的用户名和密码有误') return wrapper @yanzheng
def index():
print('欢迎来到京东商城') @yanzheng
def jiaose(name):
print('进入%s页面' % (name)) @yanzheng
def shopping_car(name):
print('%s购物车里面有[%s,%s,%s]' % (name, '飞机', '火箭', '娃娃')) index()
jiaose('管理员')
shopping_car('产品经理')
Python学习第十一课——装饰器的更多相关文章
- python学习日记(函数--装饰器)
楔子 前提,我有一段代码(一个函数). import time def run_time(): time.sleep(0.1) print('我曾踏足山巅') 需求1:现在,我想计算这段代码的运行时间 ...
- python第二十六课——装饰器
装饰器是闭包的一种使用场景: python中的装饰器在定义上需要传入一个函数对象, 在此函数执行之前或者之后都可以追加其它的操作, 这样做的好处是,在不改变源码(原本业务逻辑的)同时,进行功能的扩展: ...
- 【Python学习之二】装饰器
装饰器 首先,给出装饰器的框架: def log(func): def wrapper(*args, **kw): print('call %s():' % func.__name__) return ...
- python 学习笔记7(装饰器)
闭包(closure)是函数式编程的重要的语法结构. 定义:如果在一个内部函数里,对在外部作用域(但不是在全局作用域)的变量进行引用,那么内部函数就被认为是闭包(closure). def outer ...
- Python 学习笔记9(装饰器,decorator)
31 装饰器 装饰器可以对一个函数.方法或者类进行加工,是一种高级的python语法. 装饰函数 接收一个可调用对象作为输入参数,并返回一个新的可调用对象. 把函数传递给装饰器,然后增加新的功能,返回 ...
- Python学习笔记九:装饰器,生成器,迭代器
装饰器 本质是函数,装饰其他函数,为其他函数添加附加功能 原则: 1不修改原函数的源代码 2不修改原函数的调用方式 知识储备: 1函数即变量 使用门牌号的例子说明函数,调用方式与变量一致 2高阶函数 ...
- Python学习第二阶段,day1, 装饰器,生成器,迭代器
装饰器 不得不说,这是对初学者最难以理解的概念了,虽然我学过面向对象,但还是被搞懵逼了..前面还好理解,主要是后面“装饰器的装饰器”我理解不了.装饰器工厂,根据传入的参数不同去返回不同的装饰器,我不得 ...
- Python学习之--函数/生成器/装饰器
Function,函数,主要是为了:1提高代码的复用程度,2将程序模块化. 定义函数 在Python中,使用def 用来定义函数,一般函数的定义如下: def name(arg1,arg2,....) ...
- Python学习之路7☞装饰器
一:命名空间与作用域 1.1命名空间 局部命名空间: def foo(): x=1 def func(): pass 全局命名空间: import time class ClassName:pass ...
随机推荐
- Google 开源的依赖注入库,比 Spring 更小更快!
Google开源的一个依赖注入类库Guice,相比于Spring IoC来说更小更快.Elasticsearch大量使用了Guice,本文简单的介绍下Guice的基本概念和使用方式. 学习目标 概述: ...
- Linux控制服务和守护进程
目录 控制服务和守护进程 1.systemd 1.1.systemd简介 1.2.systemd的新特性 1.3.systemd的核心概念Unit 2.使用systemctl管理服务 控制服务和守护进 ...
- 吴裕雄 人工智能 java、javascript、HTML5、python、oracle ——智能医疗系统WEB端智能分诊代码简洁版实现
<%-- Document : getInfo Created on : 2018-10-7, 21:36:37 Author : acer --%> <%@page import= ...
- TensorFlow卷积神经网络实现手写数字识别以及可视化
边学习边笔记 https://www.cnblogs.com/felixwang2/p/9190602.html # https://www.cnblogs.com/felixwang2/p/9190 ...
- JSP技术(一)
Servlet的两个缺点: 1.首先,写在Servlet中所有的HTML标签必须包含JAVA字符串,使得处理HTTP响应报文工作十分繁琐. 2.所有的文件和HTML标记是硬代码,导致即使是微小变化,也 ...
- Servlet_001 我的第一个servlet程序
今天开启servlet学习 一.第一个Servlet程序 首先写我们的第一个servlet程序 第一步:新建我们的servlet程序(Web Project),命名为Servlet_001 第二步 : ...
- python导入openpyxl报错问题,终于解决啦
问题:折腾了一上午,安装.卸载openpyxl多次,cmd中明明显示安装成功,可python文件import时就是报错 1.安装openpyxl后,python文件导入一直报错,经过一上午的努力,终于 ...
- php 基础 自动类型转换
1.自动类型转换:表示运算的时候,Boolean,Null,String等类型,会先自动转为Integer或Float类型 null-->0 true-->1 false-->0 S ...
- 爬虫,工具 - Splash
What is it? Splash is a javascript rendering service. It's a lightweight web browser with an HTTP AP ...
- Plastic Bottle Manufacturer - Different Cosmetic Plastic Bottle Materials, Different Characteristics
Plastic bottles are usually made of PP, PE, K, AS, abs, acrylic, PET, and the like. Dust caps for th ...