小编joj*_*w85的帖子

类方法和函数的装饰器

我有一个装饰师

def deco(func):
    def inner(params):
        #< DO STUFF WITH func >
    return inner
Run Code Online (Sandbox Code Playgroud)

和一个基类

class GenericClass:
    def __init__(self,value):
        self.value = value
    def method(self,params):
        print 'NOT IMPLEMENTED YET'
    def other_method(self):
        print 'GOOD TO GO'
Run Code Online (Sandbox Code Playgroud)

我希望能够在 GenericClass 的子类上装饰“method”方法,例如检查输入/输出或处理异常(方法“method”将被覆盖)

我想做的是...

class ChildClass(GenericClass):
    @deco
    def method(self,params):
        #< NEW METHOD >
Run Code Online (Sandbox Code Playgroud)

我不是专家Python开发人员,该级别的所有文档都非常混乱(即元类、装饰器的微妙之处、__call__方法等),而且我没有找到解决方案。

python decorator

5
推荐指数
1
解决办法
5464
查看次数

标签 统计

decorator ×1

python ×1