如何将这些修饰函数包装到一个类中?

2024-09-28 22:19:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将slackapi的V2封装到一个类中,这样就可以封装有关bot的信息。下面是他们的一个示例片段:

import slack

slack_token = os.environ["SLACK_API_TOKEN"]
rtmclient = slack.RTMClient(token=slack_token)

@slack.RTMClient.run_on(event='message')
def say_hello(**payload):
    data = payload['data']
    if 'Hello' in data['text']:
        channel_id = data['channel']
        thread_ts = data['ts']
        user = data['user']

        webclient = payload['web_client']
        webclient.chat_postMessage(
            channel=channel_id,
            text="Hi <@{}>!".format(user),
            thread_ts=thread_ts
        )

rtmclient.start()

我在这里的理解是,这个say_hello函数由于decorator被传递到slack对象中,所以如果我要将它包装到一个类中,那么这个函数实际上并不在我的类中。如何包装say_hello函数,使其能够调用属于类实例的方法和引用属性?你知道吗


Tags: 函数texttokenidhellodatachannelslack
1条回答
网友
1楼 · 发布于 2024-09-28 22:19:47

看看装饰师是怎么工作的!你知道吗

def decorator_factory(f):                                                                                                                                                                     
    def decoration(*args, **kwargs):                                                                                                                                                          
        print('before')                                                                                                                                                                       
        r = f(*args, **kwargs)                                                                                                                                                                
        print('after')                                                                                                                                                                        
        return r                                                                                                                                                                              
    return decoration                                                                                                                                                                         

@decorator_factory                                                                                                                                                                            
def inc(i):                                                                                                                                                                                   
    '''                                                                                                                                                                                       
    >>> inc(23)                                                                                                                                                                               
    before                                                                                                                                                                                    
    after                                                                                                                                                                                     
    42                                                                                                                                                                                        
    '''                                                                                                                                                                                       
    return i + 1

也许有一个更好的,规范的方法来实现你想要的,但这可以做到:

class Client():                                                                                                                                                                               

    def __init__(self):                                                                                                                                                                       
        slack.RTMClient.run_on(event='message')(self.decorated)                                                                                                                               

    def decorated(self, x, y, z):                                                                                                                                                                      
        pass            

相关问题 更多 >