Key the cache by char and style

Step 5: Key the cache by char and style

Intrinsic state can be more than one field. Suppose a glyph also carries a style, such as bold or italic. Bold "a" and italic "a" are genuinely different shapes, so they must be different shared flyweights, but every bold "a" everywhere should still be one shared object.

Update GlyphFactory.get(char, style) to cache by the (char, style) pair. The default style is "regular". Same char and same style return the identical object; same char with a different style returns a different object. count() then reports unique (char, style) combinations.

So get("a", "bold") is get("a", "bold"), but get("a", "bold") is not get("a", "italic").

Hints
Starter code
class Glyph:
    def __init__(self, char, style="regular"):
        self.char = char
        self.style = style

    def render(self, position):
        return f"{self.char}@{position}"


class GlyphFactory:
    def __init__(self):
        self._cache = {}

    def get(self, char, style="regular"):
        """Return a shared Glyph keyed by the (char, style) pair.

        TODO: cache and reuse one glyph per (char, style) combination.
        """
        # TODO: use (char, style) as the cache key
        pass

    def count(self):
        return len(self._cache)


class Document:
    def __init__(self, factory):
        self.factory = factory

    def layout(self, text):
        return [self.factory.get(c) for c in text]
Write it on a laptop

The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.