【Python】Python怎么将两个功能相同的装饰器高阶函数,一个带参数一个不带参数的合并成一个装饰器?请前辈指点一二,十分感谢?

Python的装饰器怎么将两个功能相同的函数,一个带参数一个不带参数的合并成一个函数?

获取本地时间方法

def get_now_local_time():

return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

装饰器1

def log1(func):

@functools.wraps(func) # 保持传入的函数名称不被返回函数改变

def wrapper(*args, **kwargs):

print('%s %s():' % ("begin call", func.__name__))

call_func_ref = func(*args, **kwargs)

print('%s %s():' % ("end call", func.__name__))

return call_func_ref

return wrapper

@log1       # 为函数添加无参数装饰器

def now():

print('Current local time:' + get_now_local_time())

now()

无参装饰器函数调用输出

begin call now():

Current local time:2016-10-12 23:51:11

end call now():

装饰器2

def log2(text=None):

def decorator(func):

@functools.wraps(func) # 保持传入的函数名称不被返回函数改变

def wrapper(*args, **kwargs):

print('%s %s():' % (("begin " + text), func.__name__))

call_func_ref = func(*args, **kwargs)

print('%s %s():' % (("end " + text), func.__name__))

return call_func_ref

return wrapper

return decorator

@log2('execute current the function')    # 为函数添加带参数装饰器

def now():

print('Current local time:' + get_now_local_time())

now()

带有参装饰器函数调用输出

begin execute current the function now():

Current local time:2016-10-12 23:49:25

end execute current the function now():

想做完成这两个函数的合并编码,该怎么写?请大牛前辈给指点一下呗,谢谢~~~

回答

# -*- coding: utf-8 -*-

import functools

import time

def get_now_local_time():

return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

def log(text):

def decorator(func):

@functools.wraps(func)

def wrapper(*arg, **kwarg):

print('%s, %s()' % ('begin call',func.__name__) if callable(text) else \

'%s %s():' % (("begin " + text), func.__name__))

func(*arg, **kwarg)

print('%s, %s()' % ('end call', func.__name__) if callable(text) else \

'%s %s():' % (("end " + text), func.__name__))

return wrapper

return decorator(text) if callable(text) else decorator

@log

def now():

print('Current local time:' + get_now_local_time())

now()

print('==' * 50)

@log('execute current the function') # 为函数添加带参数装饰器

def now():

print('Current local time:' + get_now_local_time())

now()

需求是不是这样子的

【Python】Python怎么将两个功能相同的装饰器高阶函数,一个带参数一个不带参数的合并成一个装饰器?请前辈指点一二,十分感谢?

callable(object)的文档解释

【Python】Python怎么将两个功能相同的装饰器高阶函数,一个带参数一个不带参数的合并成一个装饰器?请前辈指点一二,十分感谢?

from types import FunctionType

def log2(*params):

assert (len(params)==1)

if type(params[0])==FunctionType:

func=params[0]

@functools.wraps(func) # 保持传入的函数名称不被返回函数改变

def wrapper(*args, **kwargs):

print('%s %s():' % (("begin call"), func.__name__))

call_func_ref = func(*args, **kwargs)

print('%s %s():' % (("end call"), func.__name__))

return call_func_ref

return wrapper

else:

text=params[0]

def decorator(func):

@functools.wraps(func) # 保持传入的函数名称不被返回函数改变

def wrapper(*args, **kwargs):

print('%s %s():' % (("begin " + text), func.__name__))

call_func_ref = func(*args, **kwargs)

print('%s %s():' % (("end " + text), func.__name__))

return call_func_ref

return wrapper

return decorator

@log2 # 为函数添加无参数装饰器

def now():

print('Current local time:' + get_now_local_time())

@log2('execute current the function') # 为函数添加带参数装饰器

def now2():

print('Current local time:' + get_now_local_time())

now()

now2()

输出结果:

begin call now():

Current local time:2016-10-13 15:12:31

end call now():

begin execute current the function now2():

Current local time:2016-10-13 15:12:31

end execute current the function now2():

可实现在没有参数时不用加括号的形式。

def log2(text='call'):

def decorator(func):

@functools.wraps(func) # 保持传入的函数名称不被返回函数改变

def wrapper(*args, **kwargs):

print('%s %s():' % (("begin " + text), func.__name__))

call_func_ref = func(*args, **kwargs)

print('%s %s():' % (("end " + text), func.__name__))

return call_func_ref

return wrapper

return decorator

@log2('execute current the function') # 为函数添加带参数装饰器

def now():

print('Current local time:' + get_now_local_time())

now()

@log2() # 为函数添加无参数装饰器

def now():

print('Current local time:' + get_now_local_time())

now()

以上是 【Python】Python怎么将两个功能相同的装饰器高阶函数,一个带参数一个不带参数的合并成一个装饰器?请前辈指点一二,十分感谢? 的全部内容, 来源链接: utcz.com/a/81075.html

回到顶部