Exercise 1: Tracking Total Calls vs. Actual Loads — Possible Solution ==================================================================== THE CHANGE ------------------------------ class ImageProxy: def __init__(self, filename): self.filename = filename self._real_image = None self.preload_count = 0 # new def display(self): self.preload_count += 1 # new — counts every call if self._real_image is None: self._real_image = RealImage(self.filename) return self._real_image.display() preload_count increments on every single call to display(), regardless of whether the real image already exists. RealImage.load_count (from this chapter's own RealImage class, unchanged) still only increments inside RealImage.__init__ - which only ever runs once per proxy, the first time _real_image is None. VERIFYING BOTH COUNTS AFTER FOUR CALLS ------------------------------ Calling proxy.display() four times in a row on the same ImageProxy gives: display() calls (preload_count): 4 actual RealImage loads (load_count): 1 WHY THE TWO NUMBERS DIVERGE ------------------------------ preload_count measures how many times the CLIENT asked to see the image - every one of those requests is genuine and gets a correct response. load_count measures how many times the EXPENSIVE underlying work (creating a RealImage) actually happened - the proxy satisfies requests 2, 3, and 4 using the same already-loaded RealImage from request 1, so only request 1 shows up in load_count. This is exactly the point of a virtual proxy: the client-facing call count and the real expensive-work count are allowed to be completely different numbers. WHY THIS WORKS AS AN ANSWER ------------------------------ The new preload_count is incremented unconditionally at the very top of display(), before the lazy-loading check runs, so it faithfully counts every client call rather than only the ones that trigger a real load - and the two counts are verified together, with the difference between them explained directly rather than left unexplained.