设为首页 加入收藏

TOP

python 之 函数 装饰器(一)
2019-06-12 00:06:09 】 浏览:105
Tags:python 函数 装饰

5.8 装饰器

1 开放封闭原则 软件一旦上线后,就应该遵循开放封闭原则,即对修改源代码是封闭的,对功能的扩展是开放的 也就是说我们必须找到一种解决方案: 能够在不修改一个功能源代码以及调用方式的前提下,为其加上新功能 原则如下: 1、不修改源代码 2、不修改调用方式 目的: 在遵循1和2原则的基础上扩展新功能

装饰器: 装饰器即在不修改被装饰对象源代码与调用方式的前提下,为被装饰器对象添加新功能,装饰器与被装饰的对象均可以是任意可调用的对象

装饰器 ===》函数 被装饰的对象 ===》函数

5.81 无参装饰器举例

import time
def index():
   time.sleep(3)
   print('welcome to index page')
def outter(func): #func=最原始的index
   def wrapper():
       start_time=time.time()
       func()
       stop_time=time.time()
       print(stop_time-start_time)
   return wrapper
?
index=outter(index) # 新的index=wrapper
index()   #wrapper()
welcome to index page
3.0000429153442383

5.82 无参装饰器升级

import time
def index():
   time.sleep(1)
   print('welcome to index page')
   return 122
def home(name):
   time.sleep(2)
   print('welcome %s to home page' %name)
#==============装饰器
def timmer(func): #func=最原始的index
   def wrapper(*args,**kwargs):
       start_time=time.time()
       res=func(*args,**kwargs) #调用最原始的index
       stop_time=time.time()
       print(stop_time-start_time)
       return res
   return wrapper
?
index=timmer(index) # 新的index=wrapper
home=timmer(home) #新的home=wrapper
# ==========================================
?
res=index() #res=wrapper()
print(res)
res=home(name='egon') #res=wrapper(name='egon')
print(res)

5.83 无参装饰器模板:

def index():
   pass
#==============装饰器
def outer(func):
   def inner(*args,**kwargs):
       res=func(*args,**kwargs)
       return res
   return inner
?
index=outer(index)
# ==========================================
res=index()
print(res)

使用:在被装饰对象正上方单独一行,@无参装饰器名

@无参装饰器名
def foo():
   pass

5.84 装饰器语法糖

import time
def timmer(func):
   def wrapper(*args,**kwargs):
       start_time=time.time()
       res=func(*args,**kwargs)
       stop_time=time.time()
       print(stop_time-start_time)
       return res
   return wrapper
?
@timmer #index=timmer(index)
def index():
   time.sleep(1)
   print('welcome to index page')
   return 122
?
@timmer # home=timmer(home)
def home(name):
   time.sleep(2)
   print('welcome %s to home page' %name)
?
index()
home('egon')

5.85 叠加装饰器

import time
current_user={
   'username':None,
}
?
def auth(func): # func=index
   def wrapper(*args,**kwargs):
       if current_user['username']:
           print('已经登陆过了')
           res=func(*args,**kwargs)
           return res
       uname=input('用户名>>: ').strip()
       pwd=input('密码>>: ').strip()
       if uname == 'egon' and pwd == '123':
           print('登陆成功')
           current_user['username']=uname
  &nbs
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Django框架简介,wsgiref 与 jinja.. 下一篇序列化,pickle,shelve,json,conf..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目