#!/usr/bin/env python3
from functools import wraps
import time
def decor1(param):
'''
支持@decor1、@decor1('text')调用
'''
def is_func():
func = param
@wraps(func)
def wrapper1(*args, **kw):
print('call start')
ts = time.time()
res = func(*args, **kw)
te = time.time()
print('call end')
print('run time:%s s'% (te -ts))
return res
return wrapper1
def is_str():
text = param
def text_deco(func):
@wraps(func)
def wrapper1(*args, **kw):
print(text)
print('call start')
ts = time.time()
res = func(*args, **kw)
te = time.time()
print('call end')
print('run time:%s s'%(te - ts))
return res
return wrapper1
return text_deco
return is_str() if isinstance(param, str) else is_func()
@decor1
def hello(string):
time.sleep(1.51)
print('hello %s'% string)
@decor1('Decorator with parameters')
def hello2(string):
time.sleep(2.222)
print('hello %s'% string)
hello('dog')
print('-' * 20)
hello2('cat')
版权声明:本文为qq_41572664原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。