Im just playing around with decorators in python and enjoying the experience, to me a decorator class seems cleaner to me than decorator functions so I have been playing with this design
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'
Problem with the above code is that it returns
------------------------------------------------------------
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
Im wondering why I can call int() like a function, and it has a callable method but it says this here as its NoneType and not callable.
Any ideas?
EDIT: Please no more answers the problem has been solved and I understand what I was doing wrong. Its a typo and im defining a function not a class
Your
returns()function returns nothing. Did you mean to make it a class?