【问题标题】:Monkey Patching in Python using super.__init__在 Python 中使用 super.__init__ 进行猴子修补
【发布时间】:2022-01-19 12:31:48
【问题描述】:

我想用我自己的班级修补其他班级。 我尝试使用Tomonkeypatch.some_func = some_func。它可以工作,但我想要一个简洁的方法(即类)。

我正在尝试猴子补丁Message Object in pyrogram

这是我的代码:

import pyrogram

class Message(pyrogram.types.messages_and_media.Message):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    @property
    def test(self):
       return "test"

当我在处理程序中使用代码时

from pyrogram import filters

@client.on_message(filters.private)
async def sometest(client, message):
    s = message.test
    await message.reply(s)
    

我明白了:

AttributeError: 'Message' object has no attribute 'test' 

但是,我用热解图修补了猴子?那为什么?

提前谢谢你!

【问题讨论】:

  • 我不太清楚你在期待什么。您的代码定义了一个 new 类,它恰好派生自 Message;它不会修补或替换 Message
  • @MisterMiyagi,我明白了。那你希望我怎么用呢?

标签: python monkeypatching python-class pyrogram


【解决方案1】:

您必须修改 pyrogram 使用的原始 Message 对象。

from pyrogram.types import Message 

@property
def test(self):
       return "test"

Message.test = test

如果您真的想在更改子类时更新 Message 类(不推荐!),您可以这样做:

from pyrogram.types import Message 

Message.__init_subclass__ = classmethod(lambda sub: [setattr(Message, k, v) for (k,v) in sub.__dict__.items() if k[:2] != '__'])

class MyMessage(Message):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    @property
    def test(self):
        return "test"

【讨论】:

    【解决方案2】:

    您可能应该创建一个新文件,导入 pyrogram.types.Message,并创建它的子类。

    from pyrogram.types import Message
    
    class MyMessage(Message):
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
    
        @property
        def test(self):
            return "test"
    

    然后您可以导入自己的MyMessage 而不是 Pyrograms 消息并像往常一样使用它。

    【讨论】:

    • 好的,谢谢。但是你能告诉我我将如何使用上面给出的例子吗?我有点困惑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    • 2015-03-26
    • 1970-01-01
    相关资源
    最近更新 更多