python学习-42 装饰器 --- 函数闭包1
函数闭包举例:
def father(name):
print('hello world')
def son():
print('儿子说:我的爸爸是%s' % name)
def grandfson():
print('孙子说:我的爷爷是%s' % name)
grandfson()
son()
father('小明')
运行结果:
hello world
儿子说:我的爸爸是小明
孙子说:我的爷爷是小明 Process finished with exit code 0
函数的包: 就是嵌套里的一层一层的函数
闭: 就是封装的意思
----------函数闭包的装饰器基本实现
import time # 装饰器框架
def timmer(func):
def wrapper():
start_time = time.time()
func()
stop_time = time.time()
print('运行时间为;%s' %(stop_time-start_time))
return wrapper @timmer
def test():
time.sleep(1)
print('test函数运行完毕') # test =timmer(test) 相当于 @timmer
test()
运行结果;
test函数运行完毕
运行时间为;1.000901460647583 Process finished with exit code 0
---------函数闭包加上返回值
import time # 装饰器框架
def timmer(func):
def wrapper():
start_time = time.time()
res = func() # 就是在运行test()
stop_time = time.time()
print('运行时间为;%s' %(stop_time-start_time))
return res
return wrapper @timmer # test = timmer(test)
def test():
time.sleep(1)
print('test函数运行完毕')
return ''
res =test() #
print(res)
运行结果:
test函数运行完毕
运行时间为;1.000777244567871
132 Process finished with exit code 0
------函数闭包加上参数
import time # 装饰器框架
def timmer(func):
def wrapper(*args,**kwargs): # *args 元组的形式,最多可传3个值。 **kwargs 字典,相当于:wrapper(*(name,age,gender),**{ })
start_time = time.time()
res = func(*args,**kwargs)
stop_time = time.time()
print('运行时间为;%s' %(stop_time-start_time))
return res
return wrapper @timmer # test = timmer(test)
def test(name,age):
time.sleep(1)
print('test函数运行完毕,名字:%s 年龄: %s' % (name,age))
return '132'
ret = test('小王',20)
print(ret) @timmer
def test1(name,age,gender):
time.sleep(1)
print('test1名字:%s,年龄%s,性别:%s' %(name,age,gender))
return '321'
res =test1('小红',18,'男')
print(res)
运行结果:
test函数运行完毕,名字:小王 年龄: 20
运行时间为;1.0007729530334473
132
test1名字:小红,年龄18,性别:男
运行时间为;1.0006530284881592
321 Process finished with exit code 0
python学习-42 装饰器 --- 函数闭包1的更多相关文章
- python学习-43 装饰器 -- 函数闭包2
函数闭包为函数加上认证功能 1.登陆账号 user_dic ={'username':None,'login':False} def auth_func(func): def wrapper(*arg ...
- python学习之装饰器-
python的装饰器 2018-02-26 在了解python的装饰器之前我们得了解python的高阶函数 python的高阶函数我们能返回一个函数名并且能将函数名作为参数传递 def outer() ...
- Python学习 :装饰器
装饰器(函数) 装饰器作为一个函数,可以为其他函数在不修改原函数代码的前提下添加新的功能 装饰器的返回值是一个函数对象.它经常用于有切面需求的场景,比如:插入日志.性能测试.事务处理.缓存.权限校验等 ...
- python基础(8)-装饰器函数&进阶
从小例子进入装饰器 统计一个函数执行耗时 原始版本 import time # time模块有提供时间相关函数 def do_something(): print("do_something ...
- python学习笔记--装饰器
1.首先是一个很无聊的函数,实现了两个数的加法运算: def f(x,y): print x+y f(2,3) 输出结果也ok 5 2.可是这时候我们感觉输出结果太单一了点,想让代码的输出多一点看起来 ...
- Python学习笔记--装饰器的实验
装饰器既然可以增加原来函数的功能,那能不能改变传给原函数的参数呢? 我们实验一下,先上代码: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date ...
- 6月4日 python学习总结 装饰器复习
1. 装饰器的原理以及为什么要使用装饰器 在代码运行期间动态增加功能的方式,称之为"装饰器"(Decorator). 在不影响原代码结构的情况下为其添加功能 2. 装饰器的基本 ...
- Python学习笔记: 装饰器Decorator
介绍 装饰器是对功能函数的加强. 在原来的功能函数之外,另外定义一个装饰器函数,对原来的功能函数进行封装(wrapper)并在wrapper的过程中增加一些辅助功能. 应用场景 如下场景: 业务函数f ...
- python学习-41 装饰器 -- 高阶函数
装饰器:本质就是函数.是为其他函数添加附加功能的. 原则:1.不修改被修饰函数的源代码2.不修改被修饰函数的调用方式 --- 装饰器的知识储备 装饰器=高阶函数+函数嵌套+闭包 高阶函数 1.高阶函数 ...
随机推荐
- 【2018.08.01】(表/栈/队列/大小顶堆)学习Stark和Queue算法小记
Train Problem I As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...
- 从xml中返回的对象,和new 返回的对象时不同的。
public BigDecimal getTax() { return tax == null ? BigDecimal.ZERO : tax; } 这是自定义的一个类 对null 做出了处理. 但是 ...
- 【零基础】神经网络优化之Adam
一.序言 Adam是神经网络优化的另一种方法,有点类似上一篇中的“动量梯度下降”,实际上是先提出了RMSprop(类似动量梯度下降的优化算法),而后结合RMSprop和动量梯度下降整出了Adam,所以 ...
- spring boot + vue 前后分离实现登录功能(二)
安装 axios 进行路由转发 npm install axios --save-dev 或者 cnpm install axios --save-dev 修改 Main.js 新增 var axio ...
- jQuery 设置select,radio的值,无法自动触发绑定的change事件
一.问题 今天在对select和radio做change事件绑定后,手动设置其value值,但是不能触发change事件 二.解决 使用trigger方法手动触发
- python wmi远程数据获取
- [转][C#]dll 引用
本文转自:https://zhidao.baidu.com/question/1176198151354174139.html 首先,对应关系: C++ C#===================== ...
- The pom for XXX is missing,no dependency information available
笔者进行性能测试时,碰到如下问题 性能测试代码编写,调试通过之后.使用cmd进入项目根目录,意图打包导出项目中所有的依赖包,以便导入至jmeter工具中 cmd中使用命令:mvn dependency ...
- spring cloud+.net core搭建微服务架构
http://www.cnblogs.com/longxianghui/p/7800316.html
- java调用科大讯飞流式(websocket)语音识别接口
要使用讯飞的能力,需先注册讯飞开发平台账号(讯飞官网参见https://www.xfyun.cn/). 再创建应用,点击右上角的控制台 -> 创建新应用: 每个应用都有一个appId,由这个ap ...