I want to apply a decorator to every method in a class. I don't have source code of the class, so I can't directly apply the decorator. I want to call some function that accepts the class and adds the decorators.
But the problem is testclass.__dict__
is a mappingproxy
object and it doesn't support any assignment or modification, directly at least. So the question is how to avoid that restriction and apply decorator?
Here is code of the failed attempt:
class qwer:
def test(self):
print('test')
def decor(func):
def w(*args, **named_args):
print('decor')
func(*args, **named_args)
return w
qwer.__dict__['test'] = decor(qwer.__dict__['test'])
Error:
TypeError: 'mappingproxy' object does not support item assignment