How To Wrap These Decorated Functions Into A Class?
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
Solution 1:
Have a look at how decorators work!
defdecorator_factory(f):                                                                                                                                                                     
    defdecoration(*args, **kwargs):                                                                                                                                                          
        print('before')                                                                                                                                                                       
        r = f(*args, **kwargs)                                                                                                                                                                
        print('after')                                                                                                                                                                        
        return r                                                                                                                                                                              
    return decoration                                                                                                                                                                         
@decorator_factory                                                                                                                                                                            definc(i):                                                                                                                                                                                   
    '''                                                                                                                                                                                       
    >>> inc(23)                                                                                                                                                                               
    before                                                                                                                                                                                    
    after                                                                                                                                                                                     
    42                                                                                                                                                                                        
    '''return i + 1There may be a better, canonical way to achieve what you want, but this would do the job:
classClient():                                                                                                                                                                               
    def__init__(self):                                                                                                                                                                       
        slack.RTMClient.run_on(event='message')(self.decorated)                                                                                                                               
    defdecorated(self, x, y, z):                                                                                                                                                                      
        pass
Post a Comment for "How To Wrap These Decorated Functions Into A Class?"