Получить доступ из декоратора к полям класса
Имеется асинхронный декоратор определенный в классе. Как получить доступ к полям класса из декоратора?
class Hello(object):
template = 'Hello, {}'
def hello(fun):
@wraps(fun)
async def wrapper(text):
return await fun(self.template.format(text))
return wrapper
@hello
async def b(self, text):
print(text)
return False
n = Hello()
asyncio.get_event_loop().run_until_complete(n.b('Maxim'))
Ответы (1 шт):
Автор решения: Danis
→ Ссылка
Функция wrapper должна принимать ещё один параметр self. в fun надо передавать self
class Hello(object):
template = 'Hello, {}'
def hello(fun):
@wraps(fun)
async def wrapper(self, text):
return await fun(self, self.template.format(text))
return wrapper
@hello
async def b(self, text):
print(text)
return False
n = Hello()
asyncio.get_event_loop().run_until_complete(n.b('Maxim'))