Skip to content
dreamcode
dreamcode
Back to lesson
Practice · Py-decorators
PredictArrangeFill in
+20 XP on finish
PREDICT · READ BEFORE YOU WRITE

What does this program print?

Read the program like the computer would, then call the output before you run anything.

class StarCache:
    def __init__(self, func):
        self.func = func
        self.cache = {}
    def __call__(self, name):
        if name not in self.cache:
            self.cache[name] = self.func(name)
        return self.cache[name]

@StarCache
def get_star(name):
    return f"Star:{name}"

g1 = get_star("Sirius")
g2 = get_star("Sirius")
print(g1 == g2, len(get_star.cache))