To illustrate the problem I created a simple example:
#!/usr/bin/env python
class Person():
def __init__(self):
self.cache = {}
def get_person_age(self):
def get_age():
print "Calculating age..."
return self.age
print self.cache
return self.cache.setdefault(self.name, get_age())
def set_person(self, name, age):
self.name = name
self.age = age
p = Person()
p.set_person('andrei', 12)
for k in range(0, 5):
p.get_person_age()
I will expect that once a cache is set a function get_person_age will be never called again, but this is not true:
$ python cache_test.py
{}
Calculating age...
{'andrei': 12}
Calculating age...
{'andrei': 12}
Calculating age...
{'andrei': 12}
Calculating age...
{'andrei': 12}
Calculating age...
Function is called again and again. What's wrong?