python decorator in class with self

Solutions on MaxInterview for python decorator in class with self by the best coders in the world

showing results for - "python decorator in class with self"
Emiliano
11 May 2019
1class Dog:
2    def __init__(self, name):
3        self.name = name
4        
5    def say_name(func):
6        def decorator(*args, **kwargs):
7            # self is always the first argument
8            self = args[0]
9            print(self.name)
10            return func(*args, **kwargs)
11        return decorator
12    
13    @say_name
14    def bark(self):
15        print('woof!')