使用装饰器的猴子修补 class.__str__ 不起作用

Monkey-patching class.__str__ using decorator not working

我正在尝试通过以下装饰器对 class 的 __str__ 特殊方法进行猴子修补:

def str_patcher(klass):
    def decorator(*args, **kwargs):
        def __str__(self):
            items = {key: value for key, value in self.__dict__.items() if not key.startswith("_")}
            return f"{type(self).__qualname__}({items})"

        klass.__str__ = __str__
        return klass

    return decorator

然后,我做了一个 MyClass class 装饰 str_patcher:

@str_patcher
class MyClass:
    def __init__(self, **kwargs):
        for key, value in kwargs.items():
            setattr(self, key, value)

但是当我打印出 MyClass(a=1, b=2, c=3) 时,我希望看到 MyClass({a: 1, b: 2, c: 3}) 但我却得到了 <class __main__.MyClass>

这是为什么?

你更深入了一层。以下就够了:

def str_patcher(klass):
    def __str__(self):
        items = {key: value for key, value in self.__dict__.items() if not key.startswith("_")}
        return f"{type(self).__qualname__}({items})"
    klass.__str__ = __str__
    return klass

然后:

@str_patcher
class Foo: pass

foo = Foo()
foo.bar = "baz"
print(foo)
# Foo({'bar': 'baz'})

请注意,您将模式用于 参数化 装饰器。如果你想为每个装饰类添加动态的东西,这将很有用,但你仍然需要稍微改变它,例如:

def str_patcher(**kwargs):
    def decorator(klass):
        def __str__(self):
            items = {key: value for key, value in self.__dict__.items() if not key.startswith("_")}
            items.update(kwargs)
            return f"{type(self).__qualname__}({items})"
        klass.__str__ = __str__
        return klass
    return decorator

@str_patcher(funny="stuff")
class Foo: pass

foo = Foo()
foo.bar = "baz"
print(foo)
# Foo({'bar': 'baz', 'funny': 'stuff'})