Python--装饰器

###############################################代码1

import time
'''不是装饰器的装饰器'''

def decorator(fun):
start = time.time()
fun()# 这一行等同于 do_something()
runtime = time.time()-start
print(runtime)


def do_something():
for i in range(10):
pass
print("play game")


decorator(do_something)
################################################代码2
import time


def decorator(fun):
def wrapper():
start = time.time()
fun()
runtime = time.time()-start
print(runtime)
return wrapper


@decorator
def do_something():
for i in range(1000000):
pass
print("play game")


do_something() #先执行@后面的函数,然后遇到“合适的机会运行do_something()函数”
#此时应该明白什么是装饰起了吧! 明白@XXXX的作用了
##############################################################下面time2是带参数的方式
import time
def timeer(func):
def warpper(*args,**kwargs):
start_time=time.time()
func(*args,**kwargs)
stop_time=time.time()
return 'the func run time is %s' %(stop_time-start_time)
return warpper


@timeer #test1=timeer(time)
def time1():
time.sleep(1)
print('time1')

@timeer
def time2(name,age): #time2=time(time2)
print(name,age)


print(time1())
print(time2('wyf',22))
==========================================================
# -*- coding: utf-8 -*-
# @Time : 2018/9/19 14:27 L004-老男孩教育-Python14期VIP视频-18节-mp4\oldboy-8
# @Author : wangyafeng
# @Email : 279949848@qq.com
# @Software: PyCharm


#装饰器 本质就是函数 作用就是装饰其他函数,为其他函数添加附加功能
#一个装饰器统一为多个函数添加额外功能
# def test1():
# logger()
# return "test1"
#
#
# def test2():
# return "test2"
# logger()
#
#
# def logger():
# print("logging")
#
# print(test1())
#装饰器原则:1不能修改被装饰的函数的源代码2不能修改被装饰函数的调用方法

# import time
#
#
# def timeer(func):
# def warpper(*args,**kwargs):
# start_time=time.time()
# func()
# stop_time=time.time()
# print('the func run time is %s' %(stop_time-start_time))
# return warpper
#
#
# @timeer #test1=timeer(time)
# def time1():
# time.sleep(3)
# return 'time1'
#
#
# print(time1())

'''
实现装饰器的知识储备
1、函数即‘变量’
2、高阶函数
3、嵌套函数
高阶函数+嵌套函数==装饰器
'''

'''
高阶函数:1、把一个函数名当做实参传给另外一个函数2、返回值中包含函数名
'''

# #这个就是函数的嵌套
# def foo():
# print('foo')
# def abc():
# print('abc')
# abc()
#
# foo()

#多层嵌套

# def grandpa():
# print("grandpa")
# def dad():
# print('dad')
# def son():
# print('son')
# son()
# dad()
# grandpa()
#

# import time
# def timeer(func):
# def warpper(*args,**kwargs):
# start_time=time.time()
# func(*args,**kwargs)
# stop_time=time.time()
# return 'the func run time is %s' %(stop_time-start_time)
# return warpper
#
#
# @timeer #test1=timeer(time)
# def time1():
# time.sleep(1)
# print('time1')
#
# @timeer
# def time2(name,age): #time2=time(time2)
# print(name,age)
#
#
# print(time1())
# print(time2('wyf',22))

# import time
# def timeer(func):
# def warpper(*args,**kwargs):
# start_time=time.time()
# func(*args,**kwargs)
# stop_time=time.time()
# return 'the func run time is %s' %(stop_time-start_time)
# return warpper
#
#
# @timeer #test1=timeer(time)
# def time1():
# time.sleep(1)
# print('time1')
#
# @timeer
# def time2(name,age): #time2=time(time2)
# print(name,age)
#
#
# print(time1())
# print(time2('wyf',22))


===================================================带参数的装饰器
import time
user='wyf'
pwd='abc'


def auth(auth_type):
def out_wrapper(func):
def wrapper(*args,**kwargs):
if auth_type == 'local':
username=input("name:").strip()
password=input("password:").strip()
if user==username and pwd==password:
func(*args, **kwargs)
print("OK")
return func(*args,**kwargs)
else:
exit("NO")
elif auth_type=='ldap':
print("nonono")
return wrapper
return out_wrapper



def index():
print("welcome to index page")


@auth(auth_type="local") # home = wrapper()
def home():
print("welcome to home page")
return "from home"


@auth(auth_type="ldap")
def bbs():
print("welcome to bbs page")

index()
print(home()) #wrapper()
bbs()











posted @ 2017-12-18 17:24  王亚锋  阅读(154)  评论(0编辑  收藏  举报