簡體   English   中英

如何將這些修飾的函數封裝到一個類中?

[英]How to wrap these decorated functions into a class?

我試圖將 Slack API 的 V2 包裝到一個類中,以便我可以將有關我的機器人的信息封裝起來。 這是他們的示例片段之一:

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函數被傳遞到 slack 對象中,所以如果我將它包裝到一個類中,該函數並沒有真正位於我的類中。 我如何包裝say_hello函數以使其能夠調用屬於我的類的實例的方法和引用屬性?

看看裝飾器是如何工作的!

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            

他們的關鍵是根本不使用裝飾器。

在 Markus 的解決方案中,直接調用“run_on”函數,然后再調用“start”函數,類似如下:

rtmclient = slack.RTMClient(token=self.Token)
rtmclient.run_on(event='message')(self.handle_command)
rtmclient.start()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM