繁体   English   中英

如何为 class 定义一个装饰器来装饰 class 方法

[英]how to define a decorator for class to decorate class method

我想在 class 方法中添加一些固定变量。 但是我不想在每次使用时都传递这些变量。 我不能直接装饰 class 方法,因为我的 class 是一个子 class 并且我不能修改父 ZA2F2ED4F8EBC2CBB4C21A29 这是我想尝试的:

def callable(o):
    return hasattr(o, "__call__")

def trace(f):
    def traced(*args, **kw):
        "Print message before and after a function call."
        print("Entering", f.__name__)
        kw['new value'] = 'new value'
        result = f(*args, **kw)
        print("Leaving", f.__name__)
        return result
    return traced

def mtrace(cls):
    for key, val in cls.__dict__.items():
        if key.startswith("__") and key.endswith("__") or not callable(val):
            continue
        print('calling in mtrace', key)
        setattr(cls, key, trace(val))
        print("Wrapped", key)
    return cls


class dull:

    @classmethod
    def method1(cls, **arg):
        print("Method 1 called with arg", arg)

@mtrace
class myclass(dull):pass


myclass.method1(a='hello')

output 是:

try class method
Method 1 called with arg {'a': 'hello'}

新值不会添加到方法 1。 我不太明白 python 解释器如何调用 class 方法。 如果有人可以帮助我,非常感谢。

我尝试使用 super 重新定义父 class。 它按我的意愿工作。 但我没有使用装饰器。 但它看起来并不漂亮。

class BaseRequest:  # cannot modify this class

    @classmethod
    def http_get(cls, *vars, **kws):
        print("Method 1 called with arg", kws)

class MyRequest(BaseRequest):
    extra_vars = {}

    @classmethod
    def http_get(cls, *vars, **kws):
        kws.update(cls.extra_vars)
        super().http_get(*vars, **kws)

    @classmethod
    def set_extra_vars(cls, **kws):
        cls.extra_vars.update(kws)

MyRequest.set_extra_vars(header='my hearders')
MyRequest.http_get(a='hello')

使用 arg {'a': 'hello', 'header': 'my hearers'} 调用的输出方法 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM