I'm trying to use a cached property decorator that can take arguments.
I looked at this implementation: http://www.daniweb.com/software-development/python/code/217241/a-cached-property-decorator
from functools import update_wrapper
def cachedProperty (func ,name =None ):
if name is None :
name =func .__name__
def _get (self ):
try :
return self .__dict__ [name ]
except KeyError :
value =func (self )
self .__dict__ [name ]=value
return value
update_wrapper (_get ,func )
def _del (self ):
self .__dict__ .pop (name ,None )
return property (_get ,None ,_del )
But the problem I have is that I cannot call the decorator with the @ syntax if I want to use the parameter:
@cachedProperty(name='test') # This does NOT work
def my_func(self):
return 'ok'
# Only this way works
cachedProperty(my_func, name='test')
How to use the @ syntax with decorators arguments?
Thanks