Skip to content

Commit 0695592

Browse files
authored
Merge pull request faif#203 from aliciawyy/master
add another implementation for lazy property
2 parents c6a4c26 + 66ae38c commit 0695592

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
__pycache__
22
*.pyc
3+
.idea

creational/lazy_evaluation.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,35 @@ def __get__(self, obj, type_):
3737
return val
3838

3939

40+
def lazy_property2(fn):
41+
attr = '_lazy__' + fn.__name__
42+
43+
@property
44+
def _lazy_property(self):
45+
if not hasattr(self, attr):
46+
setattr(self, attr, fn(self))
47+
return getattr(self, attr)
48+
return _lazy_property
49+
50+
4051
class Person(object):
4152

4253
def __init__(self, name, occupation):
4354
self.name = name
4455
self.occupation = occupation
56+
self.call_count2 = 0
4557

4658
@lazy_property
4759
def relatives(self):
4860
# Get all relatives, let's assume that it costs much time.
4961
relatives = "Many relatives."
5062
return relatives
5163

64+
@lazy_property2
65+
def parents(self):
66+
self.call_count2 += 1
67+
return "Father and mother"
68+
5269

5370
def main():
5471
Jhon = Person('Jhon', 'Coder')
@@ -58,6 +75,10 @@ def main():
5875
print(u"Jhon's relatives: {0}".format(Jhon.relatives))
5976
print(u"After we've accessed `relatives`:")
6077
print(Jhon.__dict__)
78+
print(Jhon.parents)
79+
print(Jhon.__dict__)
80+
print(Jhon.parents)
81+
print(Jhon.call_count2)
6182

6283

6384
if __name__ == '__main__':
@@ -66,7 +87,11 @@ def main():
6687
### OUTPUT ###
6788
# Name: Jhon Occupation: Coder
6889
# Before we access `relatives`:
69-
# {'name': 'Jhon', 'occupation': 'Coder'}
90+
# {'call_count2': 0, 'name': 'Jhon', 'occupation': 'Coder'}
7091
# Jhon's relatives: Many relatives.
7192
# After we've accessed `relatives`:
72-
# {'relatives': 'Many relatives.', 'name': 'Jhon', 'occupation': 'Coder'}
93+
# {'relatives': 'Many relatives.', 'call_count2': 0, 'name': 'Jhon', 'occupation': 'Coder'}
94+
# Father and mother
95+
# {'_lazy__parents': 'Father and mother', 'relatives': 'Many relatives.', 'call_count2': 1, 'name': 'Jhon', 'occupation': 'Coder'}
96+
# Father and mother
97+
# 1

tests/test_lazy.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,27 @@ def setUp(self):
1111
self.John = Person('John', 'Coder')
1212

1313
def test_innate_properties(self):
14-
self.assertDictEqual({'name': 'John', 'occupation': 'Coder'},
15-
self.John.__dict__)
14+
self.assertDictEqual(
15+
{'name': 'John', 'occupation': 'Coder', 'call_count2': 0},
16+
self.John.__dict__
17+
)
1618

1719
def test_relatives_not_in_properties(self):
1820
self.assertNotIn('relatives', self.John.__dict__)
1921

2022
def test_extended_properties(self):
2123
print(u"John's relatives: {0}".format(self.John.relatives))
22-
self.assertDictEqual({'name': 'John', 'occupation': 'Coder',
23-
'relatives': 'Many relatives.'},
24-
self.John.__dict__)
24+
self.assertDictEqual(
25+
{'name': 'John', 'occupation': 'Coder',
26+
'relatives': 'Many relatives.', 'call_count2': 0},
27+
self.John.__dict__
28+
)
2529

2630
def test_relatives_after_access(self):
2731
print(u"John's relatives: {0}".format(self.John.relatives))
2832
self.assertIn('relatives', self.John.__dict__)
33+
34+
def test_parents(self):
35+
for _ in range(2):
36+
self.assertEqual(self.John.parents, "Father and mother")
37+
self.assertEqual(self.John.call_count2, 1)

0 commit comments

Comments
 (0)