Skip to content
dreamcode
dreamcode
Lesson
Practice · Decorators
  1. Predict
  2. Arrange
  3. Fill in
+20 XP on finish
PREDICT · READ BEFORE YOU WRITE

What does this program print?

Trace the program the way the computer would, then make your call before running 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))