Search code examples
pythonsetdefault

Why does setdefault evaluate default when key is set?


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?


Solution

  • 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.