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?
The setdefault() method returns the value of the item with the specified key.
The only difference is that setdefault() automatically adds any new key with a default value in the dictionary while get() does not.
The get the method allows you to avoid getting a KeyError when the key doesn't exist however setdefault the method allows set default values when the key doesn't exist.
The problem is not in setdefault
, but in a general principle in Python (as well as most languages) that all arguments must be evaluated before the function is called. That is, even before Python checks to see if the key is in the dictionary, it needs to know the value of both the key and the default value: and since the default value is the result of a function, that function must be called first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With