[Python] self in python decorators

Discussão em 'Python' iniciado por Stack, Julho 23, 2025.

  1. Stack

    Stack Membro Participativo

    I want a decorator that would add the decorated function to list, like this :

    class My_Class(object):

    def __init__(self):
    self.list=[]

    @decorator
    def my_function(self)
    print 'Hi'


    I expect my_function to be added to self.list, but I just can't write this decorator. If I try to write it inside My_Class, then I would have to use @self.decorator, and self does not exist since we're outside any function. And if I try to write it out of My_Class, then I can't retrieve self from my_function.

    I know quite similar questions exist, but they are overly complicated, and I'm just learning python and decorators.

    Continue reading...

Compartilhe esta Página