Python 链式装饰器

Python 链式装饰器,python,Python,我想把两个装修工拴起来。但有些人最终只能运行一个。有人能给我建议解决办法吗 def decorator_redis(func_name): def redis_decorator(func): @wraps(func) def redis_wrapper(*args): redis_utilty = RedisUtilties() entity_id = args[0] return

我想把两个装修工拴起来。但有些人最终只能运行一个。有人能给我建议解决办法吗

def decorator_redis(func_name):
    def redis_decorator(func):
        @wraps(func)
        def redis_wrapper(*args):
            redis_utilty = RedisUtilties()
            entity_id = args[0]
            return 'hello' + entity_id
        return redis_wrapper
    return redis_decorator

def decorator_ES(func_name):
    def ES_decorator(func):
        @wraps(func)
        def ES_wrapper(*args):
            return args[0]+ '!'
        return ES_wrapper
    return ES_decorator

@decorator_redis('find')
@decorator_ES('find')
def fetch_entity(name):
    return

print(fetch_entity('John'))

我需要打印
你好,约翰。但是能够打印出
hello John
John取决于我单独使用的装饰器。

您应该调用装饰器包装器中的装饰函数(每一个):

def decorator(函数名):
def ES_decorator(func):
@包装(func)
def ES_包装(*参数):

return func(args[0]+'!')#您的缩进似乎错误。请重新输入您的代码。为什么有装饰工厂?两个装饰器都没有使用
func\u name
参数。基本问题是两个包装器都没有调用原始的
func()
;您实际上绕过了修饰函数。
def decorator_ES(func_name):
    def ES_decorator(func):
        @wraps(func)
        def ES_wrapper(*args):
            return func(args[0]+ '!')  # <-- HERE
        return ES_wrapper
    return ES_decorator