小编CGG*_*GJE的帖子

在python装饰器中自我

我想要一个装饰器,将装饰的函数添加到列表中,如下所示:

class My_Class(object):

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

    @decorator
    def my_function(self)
       print 'Hi'
Run Code Online (Sandbox Code Playgroud)

我希望将my_function添加到self.list,但我不能写这个装饰器.如果我尝试在My_Class中编写它,那么我将不得不使用@ self.decorator,并且self不存在,因为我们在任何函数之外.如果我尝试用My_Class写出它,那么我就无法从my_function中检索self.

我知道存在相似的问题,但它们过于复杂,我只是在学习python和装饰器.

python decorator self

4
推荐指数
1
解决办法
2540
查看次数

标签 统计

decorator ×1

python ×1

self ×1