将装饰器函数合并为类
需要创建一个类来执行“合并”功能的所有操作。在课堂上,我将更改、处理并添加新的参数。
def merge(*arg, **kwarg): # get decorator args & kwargs
def func(f):
def tmp(*args, **kwargs): # get function args & kwargs
kwargs.update(kwarg) # merge two dictionaries
return f(*args, **kwargs) # return merged data
return tmp
return func
用法:
@other_decorator # return *args and **kwarg
@merge(list=['one','two','three']) # need to merge with @other_decorator
def test(*a, **k): # get merged args and kwargs
print 'args:', a
print 'kwargs:', k
Need to create a class that will do all things as the "merge" function. In class i will change, process and add new arguments.
def merge(*arg, **kwarg): # get decorator args & kwargs
def func(f):
def tmp(*args, **kwargs): # get function args & kwargs
kwargs.update(kwarg) # merge two dictionaries
return f(*args, **kwargs) # return merged data
return tmp
return func
Usage:
@other_decorator # return *args and **kwarg
@merge(list=['one','two','three']) # need to merge with @other_decorator
def test(*a, **k): # get merged args and kwargs
print 'args:', a
print 'kwargs:', k
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我完全明白你在问什么。您的实现工作正常,如果您想创建任何类型的参数化装饰器,您将无法绕过两级间接。
要合并一个类,您可以这样做
然后您可以这样做:
但是您说您想要添加更改并处理新参数。因此,大概您希望装饰器本身处于活动状态,这样您就可以执行以下操作:
在应用装饰器之后。在这种情况下,您可能不希望 merge 成为一个类,但您希望它生成一个类,以便
test
被可修改的实例替换:然后,这允许您更改关键字稍后将有一个特定的装饰函数。
您也可以对不带类的函数参数执行相同的操作:
I'm not sure I quite get what you're asking. Your implementation works fine, and you won't get around having two levels of indirection if you want to create a parametrized decorator of any kind.
To make merge a class you could do this
Then you can do this:
But you said you want to add change and process new arguments. So presumably you want the decorator itself to be live so you can do:
After the decorator has been applied. In that case you probably don't want merge to be a class, but you want it to generate a class, so that
test
is replaced by the modifiable instance:This then allows you to change the keywords on a particular decorated function later.
You could also do the same thing with function arguments without classes: