mirror of
https://github.com/django/django.git
synced 2025-09-16 23:45:38 +00:00
Fixed #22691 -- Added aliasing to cached_property.
This commit is contained in:
parent
34ba86706f
commit
71461b14ab
3 changed files with 34 additions and 3 deletions
|
@ -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
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue