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.高阶函数 ...
随机推荐
- Dockerfile HEALTHCHECK详解
Dockerfile中使用HEALTHCHECK的形式有两种: 1.HEALTHCHECK [options] CMD command(本次详细解释) 2.HEALTHCHECK NODE 意思是禁止 ...
- 【Oracle/Java】向三张表各插入百万数据,共用时18分3秒,平均每张表6分钟
三张表DDL如下: CREATE TABLE tb01 ( "ID" ,) not null primary key, "NAME" NVARCHAR2() n ...
- markdown中如何设置字体为红色?
答: 语法如下: <font color='red'> text </font>
- 深度学习框架PyTorch一书的学习-第七章-生成对抗网络(GAN)
参考:https://github.com/chenyuntc/pytorch-book/tree/v1.0/chapter7-GAN生成动漫头像 GAN解决了非监督学习中的著名问题:给定一批样本,训 ...
- 003-结构型-01-适配器模式(Adapter)
一.概述 将一个类的接口转换成客户期望的另一个接口.适配器模式让那些接口不兼容的类可以一起工作. 1.1.适用场景 已经存在的类,它的方法和需求不匹配时(方法结果相同或相似) 不是软件设计阶段考虑的设 ...
- 123457123456#0#-----com.twoapp.YiZhiFanPai08--前拼后广--儿童益智记忆翻牌jiemei
com.twoapp.YiZhiFanPai08--前拼后广--儿童益智记忆翻牌jiemei
- PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)
1032 Sharing (25 分) To store English words, one method is to use linked lists and store a word let ...
- vs2015配置link.exe环境变量
https://www.cnblogs.com/johnwii/p/4966086.html
- 【leetcode_easy】589. N-ary Tree Preorder Traversal
problem 589. N-ary Tree Preorder Traversal N叉树的前序遍历 首先复习一下树的4种遍历,前序.中序.后序和层序.其中,前序.中序和后序是根据根节点的顺序进行区 ...
- vim 中与编码有关的选项
在 Vim 中,有四个与编码有关的选项,它们是:fileencodings.fileencoding.encoding 和 termencoding.在实际使用中,任何一个选项出现错误,都会导致出现乱 ...