接受参数和更改类型的Python装饰器类

接受参数和更改类型的Python装饰器类,python,int,decorator,Python,Int,Decorator,我只是在玩python中的decorator并享受这种体验,对我来说,decorator类似乎比decorator函数更干净,所以我一直在玩这种设计 def returns(object): """Enforces return to a specific type""" def __init__(self, returns): self.returns = returns def __call__(self, func): def wr

我只是在玩python中的decorator并享受这种体验,对我来说,decorator类似乎比decorator函数更干净,所以我一直在玩这种设计

def returns(object):
    """Enforces return to a specific type"""
    def __init__(self, returns):
        self.returns = returns

    def __call__(self, func):
        def wrapped(*args):
            out = func(*args)
            return self.returns(out)
        return wrapped

@returns(int) 
def test():
    return '4'
上述代码的问题在于它返回

------------------------------------------------------------
Traceback (most recent call last):
  File "", line 1, in <module>
  File "", line 122, in runfile
    execfile(filename, glbs)
  File "", line 33, in <module>
    @returns(int)
TypeError: 'NoneType' object is not callable
------------------------------------------------------------
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第122行,在runfile中
execfile(文件名,glbs)
文件“”,第33行,在
@返回值(整数)
TypeError:“非类型”对象不可调用
我想知道为什么我可以像调用函数一样调用int(),它有一个可调用的方法,但它在这里说它是非类型的,不可调用

有什么想法吗


编辑:请不要再回答问题了,我明白我做错了什么。这是一个输入错误,我定义的是一个函数而不是一个类

您的
返回()
函数不返回任何内容。你想让它成为一门课吗?

也许你想取代它

def returns(object):


我要去砸我的头。谢谢你发现了这一点,通常我不会犯那样的根本性错误
class returns(object):