从字符串添加装饰器

Add decorator from string

我有这样一个装饰器:

def foo(func):
    """Will print the args and kwargs of a func"""
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        print('Do I have args?')
        print(args, kwargs, sep='\n')

        return func(*args, **kwargs)

    return wrapper

还有一个 'decorator maker' 这样的:

def add_checks(*decorators):
    """Adds decorators to a func"""
    def check(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            # how do I add here the `*decorators`?
            return func(*args, **kwargs)

        return wrapper

    return check

如何通过字符串添加装饰器?所以它是这样的:

@add_checks('foo')
def bar(a, b, c=1):
    return (a * b) / c

print(bar(5, 2, c=5))
>>> Do I have args?
>>> [5, 2]
>>> {'c': 5}
>>> 2

你只需要遍历 *decorators:

import functools


def foo(func):
    """Will print the args and kwargs of a func"""
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        print('Do I have args?')
        print(args, kwargs, sep='\n')
        #return func(*args, **kwargs)

    return wrapper


def add_checks(*decorators):
    """Adds decorators to a func"""
    def check(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            for dec in decorators:
                if isinstance(dec, str):
                    # lookup for the function in globals()
                    globals()[dec](func)(*args, **kwargs)
                else:
                    dec(func)(*args, **kwargs)

            return func(*args, **kwargs)

        return wrapper

    return check


@add_checks(foo)  # reference to the function!
def bar(a, b, c=1):
    return (a * b) / c


@add_checks("foo")
def bar_strDecorated(a, b, c=1):
    return (a * b) / c


print(bar(5, 2, c=5))
print(bar_strDecorated(5, 2, c=5))

输出:

Do I have args?
(5, 2)
{'c': 5}
2.0
Do I have args?
(5, 2)
{'c': 5}
2.0