How can I send unknown list of arguments to a python decorator — when the decorator is a method of a different class?

∥☆過路亽.° 提交于 2019-12-12 04:47:05

问题


This question is related to another question I just asked.

I have created a python decorator as shown below. I want this decorator to accept an unknown list of arguments. But there is an added wrinkle. The decorator is an instance method of another class:

#!/usr/bin/env python
from functools import wraps


class A:

    def my_decorator(self, func=None, **kwargs):

        def inner_function(decorated_function):

            def wrapped_func(*fargs, **fkwargs):
                print kwargs
                return decorated_function(*fargs, **fkwargs)
            return wrapped_func

        if func:
            return inner_function(func)
        else:
            return inner_function

class B:
    my_a = A()

    @my_a.my_decorator(arg_1="Yolo", arg2="Bolo")
    def my_func():
         print "Woo Hoo!"


my_B = B()
B.my_func()

But this code below doesn't work:

Traceback (most recent call last):
  File "./decorator_test.py", line 30, in <module>
    B.my_func()
TypeError: unbound method wrapped_func() must be called with B instance as first argument (got nothing instead)

How to fix it?


回答1:


Here's my approach to coding your decorator:

class A:
    def __init__(self, func=None, **kargs):
        self.args  = func
        self.kargs = kargs 
    def __call__(self, decorated_function): 
        def wrapper_func(*args, **kargs):
            print kargs
            return decorated_function(*args, **kwargs)
        if self.func:
            #..do something..
            return wrapper_func
        elif 'xyz' in self.kargs:
            #...do something else here..
            return wrapper_func
        else:
            ...


class B:
    @A(arg_1="Yolo", arg2="Bolo")              # A(...)(my_func)
    def my_func():
        print "Woo Hoo!"

This is a typical way of coding decorators. Class A returns a callable object when called. The callable object of class A will be called automatically for decoration. When the callable object is called, its __call__ method gets triggered and its __call__ operator overloading method returns the wrapper function after performing some customization. The wrapper function is the function that wraps logic around your original function.



来源:https://stackoverflow.com/questions/44397806/how-can-i-send-unknown-list-of-arguments-to-a-python-decorator-when-the-decor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!