Answers for "python decorator in class with self"

0

python decorator in class with self

class Dog:
    def __init__(self, name):
        self.name = name
        
    def say_name(func):
        def decorator(*args, **kwargs):
            # self is always the first argument
            self = args[0]
            print(self.name)
            return func(*args, **kwargs)
        return decorator
    
    @say_name
    def bark(self):
        print('woof!')
Posted by: Guest on August-06-2020

Python Answers by Framework

Browse Popular Code Answers by Language