Skip to content

Commit

Permalink
Made CachedProperty even faster by turning it into a non-data descrip…
Browse files Browse the repository at this point in the history
…tor.
  • Loading branch information
defnull committed Aug 20, 2011
1 parent 8ebf74e commit fa7733e
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions bottle.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,21 @@ def __delete__(self, obj):
if self.read_only: raise AttributeError("Read-Only property.")
del getattr(obj, self.attr)[self.key]

def cached_property(func):
''' A property that, if accessed, replaces itself with the computed
value. Subsequent accesses won't call the getter again. '''
#TODO: Make "del obj.attr" reset the property.
return DictProperty('__dict__')(func)
class CachedProperty(object):
''' A property that is only computed once per instance and then replaces
itself with an ordinary attribute. Deleting the attribute resets the
property. '''

def __init__(self, func):
self.func = func

def __get__(self, obj, cls):
if obj is None: return self
value = obj.__dict__[self.func.__name__] = self.func(obj)
return value

cached_property = CachedProperty


class lazy_attribute(object): # Does not need configuration -> lower-case name
''' A property that caches itself to the class object. '''
Expand Down

0 comments on commit fa7733e

Please sign in to comment.