将装饰器函数合并为类

发布于 2024-08-30 17:03:14 字数 622 浏览 5 评论 0原文

需要创建一个类来执行“合并”功能的所有操作。在课堂上,我将更改、处理并添加新的参数。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友谊不毕业 2024-09-06 17:03:14

我不确定我完全明白你在问什么。您的实现工作正常,如果您想创建任何类型的参数化装饰器,您将无法绕过两级间接。

要合并一个类,您可以这样做

class Merge(object):
    def __init__(self, **extra_kws):
        self.extra_kws = extra_kws
    def __call__(self, function):
        def _wrapper(*args, **kws):
            kws.update(self.extra_kws)
            return function(*args, **kws)
        return _wrapper

然后您可以这样做:

@Merge(foo='bar')
def test(*args, **kws):
    print *args
    print **kws

但是您说您想要添加更改并处理新参数。因此,大概您希望装饰器本身处于活动状态,这样您就可以执行以下操作:

test.extra_kws['sun'] = 'dock'

在应用装饰器之后。在这种情况下,您可能不希望 merge 成为一个类,但您希望它生成一个类,以便 test 被可修改的实例替换:

def merge(**extra_kws):
    class _Merge(object):
        def __init__(self, function):
            self.extra_kws = extra_kws
            self.function = function
        def __call__(self, *args, **kws):
            kws.update(self.extra_kws)
            return self.function(*args, **kws)
    return _Merge

@merge(foo='bar')
def test(*args, **kws):
    print 'args:', args
    print 'kws:', kws

test(sun='dock')
test.extra_kws['trog'] = 'cube'
test(sun='dock')

然后,这允许您更改关键字稍后将有一个特定的装饰函数。

您也可以对不带类的函数参数执行相同的操作:

def merge(**extra_kws):
    def _decorator(function):
        def _wrapper(*args, **kws):
            kws.update(_wrapper.extra_kws)
            return function(*args, **kws)
        _wrapper.extra_kws = extra_kws
        return _wrapper
    return _decorator

@merge(foo='bar')
def test(*args, **kws):
    print 'kws:', kws

test(sun='dock')
test.extra_kws['trog'] = 'cube'
test(sun='dock')

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

class Merge(object):
    def __init__(self, **extra_kws):
        self.extra_kws = extra_kws
    def __call__(self, function):
        def _wrapper(*args, **kws):
            kws.update(self.extra_kws)
            return function(*args, **kws)
        return _wrapper

Then you can do this:

@Merge(foo='bar')
def test(*args, **kws):
    print *args
    print **kws

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:

test.extra_kws['sun'] = 'dock'

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:

def merge(**extra_kws):
    class _Merge(object):
        def __init__(self, function):
            self.extra_kws = extra_kws
            self.function = function
        def __call__(self, *args, **kws):
            kws.update(self.extra_kws)
            return self.function(*args, **kws)
    return _Merge

@merge(foo='bar')
def test(*args, **kws):
    print 'args:', args
    print 'kws:', kws

test(sun='dock')
test.extra_kws['trog'] = 'cube'
test(sun='dock')

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:

def merge(**extra_kws):
    def _decorator(function):
        def _wrapper(*args, **kws):
            kws.update(_wrapper.extra_kws)
            return function(*args, **kws)
        _wrapper.extra_kws = extra_kws
        return _wrapper
    return _decorator

@merge(foo='bar')
def test(*args, **kws):
    print 'kws:', kws

test(sun='dock')
test.extra_kws['trog'] = 'cube'
test(sun='dock')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文