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!
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
Solution 2:
They key is not to use the decorator at all.
From Markus' solution, just call the "run_on" function directly, before you call "start" function, similar to the below:
rtmclient = slack.RTMClient(token=self.Token)
rtmclient.run_on(event='message')(self.handle_command)
rtmclient.start()
Post a Comment for "How To Wrap These Decorated Functions Into A Class?"