装饰器( decorate )
装饰器分步解释-形成过程:
#-*- coding: UTF-8 -*- #示例1:
def deco(p_args):
def pack():
print('haha,i am deco fun')
print('i want to use parent fun arg: '+p_args)
print('haha,i am deco fun--finished\n')
return pack deco('abc') #执行结果无返回值
deco('abc')() #执行结果同示例2 #示例2:
def deco(p_args):
def pack():
print('haha,i am deco fun')
print('i want to use parent fun arg: '+p_args)
print('haha,i am deco fun--finished\n')
return pack() #需要加上小括号,否则pack函数不会被执行 deco('abc') #执行结果返回如下:
#haha,i am deco fun
#there are 2 args.they are:
#haha,i am deco fun--finished #示例3:
def myf():
print('i want to be decorated.') def deco(fun):
def pack():
print('haha,i am deco fun')
#print('i want to use parent fun arg: '+p_args)
fun()
print('haha,i am deco fun--finished\n')
return pack #此处不加括号,deco(myf)执行结果无返回。同示例1 deco(myf) #执行结果无返回。
deco(myf)() #执行结果同示例4 #示例4:
def myf():
print('i want to be decorated.') def deco(fun):
def pack():
print('haha,i am deco fun')
#print('i want to use parent fun arg: '+p_args)
fun()
print('haha,i am deco fun--finished\n')
return pack() #加括号,deco(myf)执行结果输出如下,同示例2 deco(myf) #将myf函数传给deco函数的参数fun。
#haha,i am deco fun
#i want to be decorated.
#haha,i am deco fun--finished #示例5:
def myf():
print('i want to be decorated.') def deco(fun):
def pack():
print('haha,i am deco fun')
#print('i want to use parent fun arg: '+p_args)
fun()
print('haha,i am deco fun--finished\n')
return pack #此处不加括号,而是在最外面的函数执行的时候再加括号执行,如deco(myf)() #deco(myf)()的执行结果等价于如下,输出结果同示例4:
myf1 = deco(myf)
myf1()
#haha,i am deco fun
#i want to be decorated.
#haha,i am deco fun--finished #此处的变量myf1跟myf没有任何关系,只是将deco(myf)这个函数赋予了变量myf1,然后再通过myf1()的方式执行该函数。所以可以将myf1重新写为myf,就成了装饰器的效果,如示例6。
myf = deco(myf)
myf() #示例6--装饰器:
def deco(fun):
def pack():
print('haha,i am deco fun----')
#print('i want to use parent fun arg: '+p_args)
fun()
print('haha,i am deco fun--finished\n')
return pack #此处不加括号,而是在最外面的函数执行的时候再加括号执行,如deco(myf)() @deco
def myf():
print('i want to be decorated,6.')
myf()
装饰器中的函数参数传递:
def deco(fun):
def pack(*args,**kwargs): #这样写可以传递任意参数。也可以直接写name,age,只是这样在其他函数调用的时候会出错,因为其他函数的参数可能并不是name,age等。。。
print('haha,i am deco fun')
print('there are %d args.they are: %s %d' %(len(args),args[0],args[1])) #调用原函数的参数
fun(*args,**kwargs)
print('haha,i am deco fun--finished')
return pack @deco #将sayhi传给deco的参数fun
def sayhi(name,age):
print('helo,i am %s ,my age is %d.'%(name,age)) sayhi('LiuXue',20)
#返回结果:
haha,i am deco fun
there are 2 args.they are: LiuXue 20
helo,i am LiuXue ,my age is.
haha,i am deco fun--finished
#定义函数:
def hello(*args,**kw):
print 'ab'
print args
print kw
args=range(1,5) hello(args) #返回值:
ab
([1, 2, 3, 4],)
{} #定义装饰函数:
def dec(fun):
def wrapper(*args,**kw):
print 'do sth. before'
fun(*args,**kw)
print 'do sth. after'
return wrapper dec(hello(args)) #将hello函数及参数当做变量赋予dec,只相当于直接执行hello(args),返回值:
ab
([1, 2, 3, 4],)
{} p=dec(hello)
p(args) dec(hello)(args) #将函数当做变量赋予dec,然后通过变量调用函数,再赋予变量变量,返回值:
do sth. before
ab
([1, 2, 3, 4],)
{}
do sth. after
def dec(fun):
def wrapper(*args,**kw):
print 'do sth. before'
fun(*args,**kw) #此处如果改为 return fun(*args,**kw),则下一句print 'after'不会再执行。在函数中,遇到第一个return则不会再执行后面的语句,如果返回两个值,可以写在同一行。如果用了return,函数执行完会得到结果,没有return则无返回值
print 'do sth. after'
return wrapper @dec #通过@调用装饰函数
def hello(*args,**kw):
print 'ab'
print args
print kw
args=range(1,5) hello(args)
装饰器自身接收参数:
# -*- coding: UTF-8 -*- def deco(darg): #装饰函数的参数
print darg
def getFun(func):
def pack(name,age): #这样写可以传递任意参数。也可以直接写name,age,只是这样在其他函数调用的时候会出错,因为其他函数的参数可能并不是name,age等。。。
print('haha,i am darg: '+darg) #装饰函数的参数可以传入使用
print('there are args.they are: %s %d' %(name,age)) #调用原函数的参数
func(name,age)
print('haha,i am deco fun--finished')
return pack
return getFun @deco('abc') #装饰函数调用参数‘abc’
def sayhi(name,age):
m=15
print('helo,i am %s ,my age is %d.'%(name,age)) name='LiuXue'
age=20
sayhi(name,age)
# #返回结果:
abc
haha,i am darg: abc
there are args.they are: LiuXue 20
helo,i am LiuXue ,my age is 20.
haha,i am deco fun--finished
装饰器( decorate )的更多相关文章
- Python装饰器基础及运行时间
一.装饰器基础 装饰器是可调用的对象,其参数是另一个函数(被装饰的函数).装饰器可能会处理被装饰的函数,然后把他返回,或者将其替换成另一个函数或可调用对象. eg:decorate装饰器 @decor ...
- 回顾Python装饰器
函数装饰器(function decorator)可以对函数进行“标注”,给函数提供更多的特性. 在理解装饰器之前需要理解闭包(closure).Python3.0 引入了保留关键字 nonlocal ...
- Fluent_Python_Part3函数即对象,07-closure-decoration,闭包与装饰器
第7章 函数装饰器和闭包 装饰器用于在源码中"标记"函数,动态地增强函数的行为. 了解装饰器前提是理解闭包. 闭包除了在装饰器中有用以外,还是回调式编程和函数式编程风格的基础. 1 ...
- python 装饰器(四):装饰器基础(三)叠放装饰器,参数化装饰器
叠放装饰器 示例 7-19 演示了叠放装饰器的方式:@lru_cache 应用到 @clock 装饰fibonacci 得到的结果上.在示例 7-21 中,模块中最后一个函数应用了两个 @htmliz ...
- Python函数装饰器高级用法
在了解了Python函数装饰器基础知识和闭包之后,开始正式学习函数装饰器. 典型的函数装饰器 以下示例定义了一个装饰器,输出函数的运行时间: 函数装饰器和闭包紧密结合,入参func代表被装饰函数,通过 ...
- Java设计模式(七)Decorate装饰器模式
一.场景描述 (一)问题 系统中最初使用Crystal Report(水晶报表)工具生成报表,并将报表发送给客户端查看,此时定义一CrystalReport工具类即可完成水晶报表的生成工作. 后续报表 ...
- python cookbook 学习系列(一) python中的装饰器
简介 装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象.它经常用于有切面需求的场景,比如:插入日志.性能测试.事务处理.缓 ...
- 循序渐进Python3(四) -- 装饰器、迭代器和生成器
初识装饰器(decorator ) Python的 decorator 本质上就是一个高阶函数,它接收一个函数作为参数,然后,返回一个新函数. 使用 decorator 用Python提供的 @ 语法 ...
- 【转】详解Python的装饰器
原文链接:http://python.jobbole.com/86717/ Python中的装饰器是你进入Python大门的一道坎,不管你跨不跨过去它都在那里. 为什么需要装饰器 我们假设你的程序实现 ...
随机推荐
- 四大组件之Activity——生命周期
1.完整的Activity生命周期 包括onCreate() -> onStart() -> onResume() -> onPause() -> onStop -> o ...
- css3基础下
box-shadow:0 5px 5px rgba(0,0,0,0.5) 文本 text-shadow:5px 5px 4px green; word-wrap: 背景: background:#ff ...
- html中设置textbox的宽和高
1.宽:width是不行的,而应该用size size=30,表示能输入30个字符 2.高: style="height:50px"
- Maven项目中Spring整合Mybatis
Maven项目中Spring整合Mybatis 添加jar包依赖 spring需要的jar包依赖 <dependency> <groupId>org.springframewo ...
- 第六章、Linux 的文件权限与目录配置
第六章.Linux 的文件权限与目录配置 1. 使用者与群组 2. Linux文件权限概念 2.1 Linux文件属性 2.2 如何改变文件属性与权限: chgrp, chown, chmod 2.3 ...
- 安装caffe(opencv3+anaconda3)
目录 仅安装CPU版本的caffe 1.下载相关的依赖包: 2.安装opencv3 3.安装caffe 参考文献: 仅安装CPU版本的caffe 1.下载相关的依赖包: sudo apt-get in ...
- vs2015 点击cshtml 后提示 "无效指针" 的解决办法
1. 关闭vs 2. 删除 %LocalAppData%\Microsoft\VisualStudio\14.0\ComponentModelCache 3. 打开vs OK 解决
- js 获取当前标签 jquery1.11.4
.<input type="checkbox" onchange='allstu(this);return false;' /> 2.<input type=&q ...
- 使用javascript获取wx.config内部字段解决微信分享
背景 在微信分享开发的时候我们通常的流程是 <?php require_once "jssdk.php"; $jssdk = new JSSDK("yourAppI ...
- SQL Serever学习5——数据库配置
数据库的主要属性 限制访问 用来设置数据允许用户访问的状态,或者说允许多少客户访问,有3个选项: MULTI_USER(多个),大多数数据库正常状态,允许多个用户同时访问该数据库. SINGLE_US ...