如何将这些装饰函数包装到一个类中? [英] How to wrap these decorated functions into a class?

查看:71
本文介绍了如何将这些装饰函数包装到一个类中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Slack API的V2包装到一个类中,以便可以封装有关我的机器人的信息。这是他们的示例片段之一:

I am attempting to wrap V2 of the Slack API into a class so that I can keep information about my bot encapsulated. Here is one of their example snippets:

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 函数由于装饰器而被传递到松弛对象中,因此,如果我将其包装到一个类中,则该函数并不是真正位于我的内部类。如何包装 say_hello 函数,使其能够调用属于我的类实例的方法和引用属性?

My understanding here is that this say_hello function is being passed into the slack object because of the decorator, so if I were to wrap this into a class, that function isn't really sitting inside my class. How do I wrap the say_hello function for it to be able to call methods and reference properties that belonged to an instance of my class?

推荐答案

看看装饰器的工作原理!

Have a look at how decorators work!

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

也许有更好的,规范的方法来实现您想要的目标,但这可以完成工作:

There may be a better, canonical way to achieve what you want, but this would do the job:

class Client():                                                                                                                                                                               

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

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

这篇关于如何将这些装饰函数包装到一个类中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆