python 装饰器demo
- 本质就是一个函数,这个函数符合闭包语法结构,可以在函数不需要改变任何代码情况下增加额外功装饰器的返回值是一个函数的引用
- 功能:1.引入日志;2.函数执行时间统计;3.执行函数前预备处理;4.执行函数后清理功能;5.权限校验;6.缓存
作用域
x =
def funx():
x =
print(x)
funx()
print(x) x =
def funx():
print(x)
funx()
print(x) x =
def funx():
def func1():
print(x)
func1()
funx()
print(x)
输出
函数名作为返回值
def outer():
def inner():
pass
return inner s = outer()
print(s)
输出
<function outer.<locals>.inner at 0x1058c60d0>
函数名可以作为一个参数
def index():
print("index func") def outer(index):
s = index
s() outer(index)
输出
index func
code
def outer():
def inner():
print("inner func excuted")
inner() # 调用执行inner()函数
print("outer func excuted")
outer() # 调用执行outer函数
输出
inner func excuted
outer func excuted
code
x =
def outer(): def inner():
print("x=%s" %x) # 引用了一个非inner函数内部的变量
print("inner func excuted")
inner() # 执行inner函数
print("outer func excuted") outer()
输出
x=
inner func excuted
outer func excuted
code
def outer():
x =
def inner():
print("x=%s" % x)
print("inner func excuted")
print("outer func excuted")
return inner # 返回内部函数名
outer()()
输出
outer func excuted
x=
inner func excuted
def outer():
x =
def inner():
print("x=%s" %x)
print("inner func excuted")
inner()
print("outer func excuted") outer()
输出
x=
inner func excuted
outer func excuted
code
def outer():
x =
y =
def inner():
print("x= %s" %x)
print("y= %s" %y) print(inner.__closure__)
return inner outer()
输出
(<cell at 0x102d6ea98: int object at 0x1009e0c80>, <cell at 0x102d6ebe8: int object at 0x1009e0ca0>)
类装饰器
class Foo(object):
def __init__(self, func):
self._func = func
def __call__(self):
print('class decorator runing')
self._func()
print('class decorator ending') @Foo
def bar():
print('bar') bar()
output
class decorator runing
bar
class decorator ending
类装饰器
class Foo(object):
def __init__(self):
pass
def __call__(self, func):
def _call(*args, **kw):
print('class decorator runing')
return func(*args, **kw)
return _call class Bar(object):
@Foo()
def bar(self, test, ids): # bar = Foo()(bar)
print('bar') Bar().bar('aa', 'ids')
output
class decorator runing
bar
装饰器的嵌套
import time
import random
def timmer(func):
def wrapper():
start_time = time.time()
func()
stop_time =time.time()
print('run time is %s' %(stop_time - start_time))
return wrapper def auth(func):
def deco():
name = input('name: ')
password = input('password: ')
if name == 'egon' and password == '':
print('login successful')
func() # wrapper()
else:
print('login err')
print("hahha")
return deco @auth # index = auth(timmer(index))
@timmer # index = timmer(index)
def index():
time.sleep()
print('welecome to index page')
index()
输出
name: egon
password:
login successful
welecome to index page
run time is 3.005250930786133
hahha
有参装饰器
import time
def outer(func): # 将index的地址传递给func
def inner(*args, **kwargs):
start_time = time.time()
func(*args, **kwargs) # fun = index 即func保存了外部index函数的地址
end_time = time.time()
print("运行时间为%s"%(end_time - start_time))
return inner # 返回inner的地址 @outer
def dd(a):
print("haha -> ",a) dd("xiaoming")
输出
haha -> xiaoming
运行时间为5.626678466796875e-
无参数装饰器
import time, random def outer(func): # 将index的地址传递给func
def inner():
start_time = time.time()
func() # fun = index 即func保存了外部index函数的地址
end_time = time.time()
print("运行时间为%s"%(end_time - start_time))
return inner # 返回inner的地址 def index():
time.sleep(random.randrange(, ))
print("welcome to index page") @outer
def indexone():
time.sleep(random.randrange(, ))
print("welcome to index page") index = outer(index) # 这里返回的是inner的地址,并重新赋值给index
index() print("\n") indexone()
输出
welcome to index page
运行时间为4. welcome to index page
运行时间为1.
有参数装饰器
import time, random def outer(func): # 将index的地址传递给func
def inner(a):
start_time = time.time()
func(a) # fun = index 即func保存了外部index函数的地址
end_time = time.time()
print("运行时间为%s"%(end_time - start_time))
return inner # 返回inner的地址 def index(b):
time.sleep(random.randrange(, ))
print("welcome to index page",b) @outer
def indexone(a):
time.sleep(random.randrange(, ))
print("welcome to index page",a) index = outer(index) # 这里返回的是inner的地址,并重新赋值给index
index("bb") print("\n") indexone("aa")
输出
welcome to index page bb
运行时间为3. welcome to index page aa
运行时间为4.
被装饰的函数有返回值
import time
import random
def timmer(func):
def wrapper(*args,**kwargs):
print("kk")
print(*args)
start_time = time.time()
res=func(*args,**kwargs) #res来接收home函数的返回值
stop_time=time.time()
print('run time is %s' %(stop_time-start_time))
return res
return wrapper @timmer
def home(name):
time.sleep(random.randrange(,))
print('welecome to %s HOME page' %name)
return print(home("haha"))
输出
kk
haha
welecome to haha HOME page
run time is 2.001023054122925
参考:
https://www.cnblogs.com/huchong/p/7725564.html#_label2
python 装饰器demo的更多相关文章
- python装饰器,迭代器,生成器,协程
python装饰器[1] 首先先明白以下两点 #嵌套函数 def out1(): def inner1(): print(1234) inner1()#当没有加入inner时out()不会打印输出12 ...
- Python装饰器使用技巧
装饰器 装饰器是程序开发中经常会用到的一个功能,用好了装饰器,开发效率如虎添翼,所以这也是Python面试中必问的问题,但对于好多初次接触这个知识的人来讲,这个功能有点绕,自学时直接绕过去了,然后面试 ...
- 关于python装饰器
关于python装饰器,不是系统的介绍,只是说一下某些问题 1 首先了解变量作用于非常重要 2 其次要了解闭包 def logger(func): def inner(*args, **kwargs) ...
- python装饰器通俗易懂的解释!
1.python装饰器 刚刚接触python的装饰器,简直懵逼了,直接不懂什么意思啊有木有,自己都忘了走了多少遍Debug,查了多少遍资料,猜有点点开始明白了.总结了一下解释得比较好的,通俗易懂的来说 ...
- Python 装饰器学习
Python装饰器学习(九步入门) 这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 1 2 3 4 5 6 7 8 # -*- c ...
- python 装饰器修改调整函数参数
简单记录一下利用python装饰器来调整函数的方法.现在有个需求:参数line范围为1-16,要求把9-16的范围转化为1-8,即9对应1,10对应2,...,16对应8. 下面是例子: def fo ...
- python 装饰器学习(decorator)
最近看到有个装饰器的例子,没看懂, #!/usr/bin/python class decorator(object): def __init__(self,f): print "initi ...
- Python装饰器详解
python中的装饰器是一个用得非常多的东西,我们可以把一些特定的方法.通用的方法写成一个个装饰器,这就为调用这些方法提供一个非常大的便利,如此提高我们代码的可读性以及简洁性,以及可扩展性. 在学习p ...
- 关于python装饰器(Decorators)最底层理解的一句话
一个decorator只是一个带有一个函数作为参数并返回一个替换函数的闭包. http://www.xxx.com/html/2016/pythonhexinbiancheng_0718/1044.h ...
随机推荐
- DEDECMS标签笔记
注意点: 1.dede的标签不可以嵌套(除了channelartlist里面可以嵌套指定的标签),那么当需要使用{dede:global.cfg_webname/}dede标签的时候我们需要转换成[f ...
- MySQL DataType--隐式类型转换
隐式类型转换 在官方文档中对隐式类型转换规则有如下描述: 1. If one or both arguments are NULL, the result of the comparison is N ...
- Nginx 配置 HTTPS(多域名)
平常开发要求比较低, 依然在用 HTTP, 但到了微信小程序就不行了, 腾讯和苹果都对 API 提出了 HTTPS 的要求. 尤其是苹果, 不仅要求 HTTPS, 还要求 TLS 协议版本要在 1.2 ...
- SQL SERVER-CROSS APPLY
CROSS APPLY和 OUTER APPLY 区别详解 SQL Server 2005 新增 cross apply 和 outer apply 联接语句,增加这两个东东有啥作用呢? 我们知道有个 ...
- 使用maven导入module时,报java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
在新装IDEA导入Flink源码时出现一些问题,在此记录,希望能帮到大伙! 一.环境 IDEA2019.1.2(破解版):OpenJDK 1.8.0_40:Maven 3.5.3/3.2.5/3.6. ...
- altium designer的pcb板如何移动到原点?
可以把所有的都选中,然后将光标移到起点处,将所有的移到原点的地方,但这种做法很多时候都不好:比较好的办法就是将原点设置到起点上来. 具体做法是:edit--origin --set. 这时光标成了十字 ...
- 洛谷 P1280 尼克的任务题解
题目链接:https://www.luogu.org/problem/P1280 题目描述 尼克每天上班之前都连接上英特网,接收他的上司发来的邮件,这些邮件包含了尼克主管的部门当天要完成的全部任务,每 ...
- Java精通并发-notify方法详解及线程获取锁的方式分析
wait(): 在上一次https://www.cnblogs.com/webor2006/p/11404521.html中对于无参数的wait()方法的javadoc进行了解读,而它是调用了一个参数 ...
- js实现点击按钮时显示弹框,点击按钮及弹框以外的区域时隐藏弹框
转自https://blog.csdn.net/yimawujiang/article/details/86496936 问题:js实现点击按钮时显示弹框,点击按钮及弹框以外的区域时隐藏弹框? 方案一 ...
- HTTP的幂等性
转自: https://www.jianshu.com/p/234cf2e96832 理解HTTP幂等性基于HTTP协议的Web API是时下最为流行的一种分布式服务提供方式.无论是在大型互联网应用还 ...