##// END OF EJS Templates
small fix for lazy property
marcink -
r2494:c4d418b4 beta
parent child Browse files
Show More
@@ -1,27 +1,28
1 class LazyProperty(object):
1 class LazyProperty(object):
2 """
2 """
3 Decorator for easier creation of ``property`` from potentially expensive to
3 Decorator for easier creation of ``property`` from potentially expensive to
4 calculate attribute of the class.
4 calculate attribute of the class.
5
5
6 Usage::
6 Usage::
7
7
8 class Foo(object):
8 class Foo(object):
9 @LazyProperty
9 @LazyProperty
10 def bar(self):
10 def bar(self):
11 print 'Calculating self._bar'
11 print 'Calculating self._bar'
12 return 42
12 return 42
13
13
14 Taken from http://blog.pythonisito.com/2008/08/lazy-descriptors.html and
14 Taken from http://blog.pythonisito.com/2008/08/lazy-descriptors.html and
15 used widely.
15 used widely.
16 """
16 """
17
17
18 def __init__(self, func):
18 def __init__(self, func):
19 self._func = func
19 self._func = func
20 self.__module__ = func.__module__
20 self.__name__ = func.__name__
21 self.__name__ = func.__name__
21 self.__doc__ = func.__doc__
22 self.__doc__ = func.__doc__
22
23
23 def __get__(self, obj, klass=None):
24 def __get__(self, obj, klass=None):
24 if obj is None:
25 if obj is None:
25 return None
26 return self
26 result = obj.__dict__[self.__name__] = self._func(obj)
27 result = obj.__dict__[self.__name__] = self._func(obj)
27 return result
28 return result
General Comments 0
You need to be logged in to leave comments. Login now