Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 994
Andrei Avatar asked Apr 01 '14 21:04

Andrei


People also ask

What does setdefault do in Python?

The setdefault() method returns the value of the item with the specified key.

What are the differences between the GET and Setdefault methods?

The only difference is that setdefault() automatically adds any new key with a default value in the dictionary while get() does not.

How is the function get () different from the Setdefault () for dictionary class?

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.


1 Answers

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.

like image 112
Daniel Roseman Avatar answered Oct 16 '22 19:10

Daniel Roseman