You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将类内多个@staticmethod装饰器合并为单个装饰器

合并类内多个静态装饰器为单个装饰器的解决方案

问题背景

需要将类内部定义的多个@staticmethod装饰器(如decorator_1、decorator_2)合并为一个组合装饰器combined_decorator,避免在每个方法上重复堆叠多个装饰器。这些装饰器需要访问类的实例对象(比如修改实例属性),因此无法在类外部定义。

尝试的实现出现NameError: name 'MyClass' is not defined错误,核心问题是类定义过程中类对象尚未完全创建,内部无法直接通过类名访问静态方法。

错误原因

在类定义的执行阶段,MyClass这个类对象还未被完全初始化,所以在_combined_decorator内部使用@MyClass._decorator1这类语法时,Python无法解析MyClass的名称。直接使用@_decorator1也会失败,因为_decorator1是staticmethod实例,装饰器语法需要的是一个接收函数的普通函数,而staticmethod的调用逻辑不匹配。

解决方法

方法1:手动调用装饰器函数(利用staticmethod的__func__属性)

在组合装饰器中,直接从staticmethod实例中取出原始装饰器函数,手动嵌套调用:

class MyClass:
    @staticmethod
    def _decorator1(func):
        def wrapper(self, *args, **kwargs):
            print("static decorator 1")
            return func(self, *args, **kwargs)
        return wrapper

    @staticmethod
    def _decorator2(func):
        def wrapper(self, *args, **kwargs):
            print("static decorator 2")
            return func(self, *args, **kwargs)
        return wrapper
        
    @staticmethod
    def _combined_decorator(func):
        # 从staticmethod对象中获取原始装饰器函数并嵌套调用
        decorated = _decorator2.__func__(func)
        decorated = _decorator1.__func__(decorated)
        return decorated

    @_combined_decorator
    def my_func(self):
        print("my func")

x = MyClass()
x.my_func()

执行后输出:

static decorator 1
static decorator 2
my func

方法2:使用classmethod实现组合装饰器

将组合装饰器改为@classmethod,通过cls参数访问类的静态方法,避免直接引用未初始化的类名:

class MyClass:
    @staticmethod
    def _decorator1(func):
        def wrapper(self, *args, **kwargs):
            print("static decorator 1")
            return func(self, *args, **kwargs)
        return wrapper

    @staticmethod
    def _decorator2(func):
        def wrapper(self, *args, **kwargs):
            print("static decorator 2")
            return func(self, *args, **kwargs)
        return wrapper
        
    @classmethod
    def _combined_decorator(cls, func):
        # 通过cls访问类的静态方法,嵌套装饰目标函数
        wrapped = cls._decorator2(func)
        wrapped = cls._decorator1(wrapped)
        return wrapped

    @_combined_decorator
    def my_func(self):
        print("my func")

x = MyClass()
x.my_func()

执行后输出与方法1一致。

内容的提问来源于stack exchange,提问作者VRichardJP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 15:58:14