开发者

Apply a list of decorators to a callable?

开发者 https://www.devze.com 2023-01-23 19:39 出处:网络
Given a list of decorator methods, how would one apply those to a callable? For example, since: @foo @bar

Given a list of decorator methods, how would one apply those to a callable?

For example, since:

@foo
@bar
def baz():
    pass

...is the same as:

def baz():
    pass
baz = foo(bar(baz)))

...one would assume that with a list of开发者_C百科 decorators ([foo, bar]) they could be applied to baz dynamically.


With yet another decorator!

def yad(decorators):
    def decorator(f):
        for d in reversed(decorators):
            f = d(f)
        return f
    return decorator

example usage

 list_of_decorators = [foo, bar]

@yad(list_of_decorators)
def foo():
    print 'foo'

Without the decorator syntax, it would look like

 func = yad(list_of_decorators)(func)

If you wanted to apply the same list to multiple functions, you can do it like:

 dec = yad(list_of_decorators)

 func1 = dec(func1)

 @dec
 def func2():
     pass

As recursive points out in the comments, you can define yad (I'm sure there's a better name for this) to accept *decorators instead of decorators. Then you don't have to use brackets if you're creating the list in situ. The way that I've demonstrated is better if the list is created elsewhere.


With an alltime classic:

def compose(f, g):
    def composed(*a, **k):
        return g(f(*a, **k))
    return composed

@compose(foo, compose(bar, baz))
def blam():
    pass
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号