mirror of
https://github.com/quantum5/punyverse.git
synced 2025-04-24 13:11:57 -04:00
19 lines
602 B
Python
19 lines
602 B
Python
class cached_property(object):
|
|
def __init__(self, func, name=None):
|
|
self.func = func
|
|
self.name = name or func.__name__
|
|
self.__doc__ = getattr(func, '__doc__')
|
|
|
|
def __get__(self, instance, owner=None):
|
|
if instance is None:
|
|
return self
|
|
result = instance.__dict__[self.name] = self.func(instance)
|
|
return result
|
|
|
|
def __set__(self, instance, value):
|
|
if value is None:
|
|
if self.name in instance.__dict__:
|
|
del instance.__dict__[self.name]
|
|
else:
|
|
instance.__dict__[self.name] = value
|