Fixed #22691 -- Added aliasing to cached_property.

This commit is contained in:
Curtis 2014-05-24 20:29:31 +10:00 committed by Tim Graham
parent 34ba86706f
commit 71461b14ab
3 changed files with 34 additions and 3 deletions

View file

@ -45,14 +45,18 @@ class cached_property(object):
"""
Decorator that converts a method with a single self argument into a
property cached on the instance.
Optional ``name`` argument allows you to make cached properties of other
methods. (e.g. url = cached_property(get_absolute_url, name='url') )
"""
def __init__(self, func):
def __init__(self, func, name=None):
self.func = func
self.name = name or func.__name__
def __get__(self, instance, type=None):
if instance is None:
return self
res = instance.__dict__[self.func.__name__] = self.func(instance)
res = instance.__dict__[self.name] = self.func(instance)
return res